Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Time duration of udp_socket.send(...)  (Read 1996 times)

0 Members and 1 Guest are viewing this topic.

Vaclav

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Time duration of udp_socket.send(...)
« on: September 04, 2013, 11:35:28 am »
Hello,
First I'd like to say SFML-Network is really neat and very easy to use, thank you for such a library.

Second I have some questions.
I'm sending an UDP packet and measuring how long it takes the function to return with an sf::Clock.
To my surprise, it takes quite long (around 4 ms) even if I set the socket non-blocking. Why is that, or can I do anything to reduce this, except for creating a thread myself? This seems quite a lot , suppose I want to send data to 15 clients over UDP, that's 60 ms already for one update, and the server also has to do physics, rendering at 60 fps and sending 10 updates per second.
To reduce the time, I have tried:
>setting the socket to blocking/non-blocking
>sending to local or public IP address (still 4 ms even for 127.0.0.1, and 2 ms for "localhost").
>receiving the packet with a client written in SFML (LOL I KNOW it doesn't matter for UDP)
>sleeping some time before sending

So any idea? What am I missing here?
My code:
#include <iostream>
#include <SFML/Network.hpp>
int main()
{
    sf::UdpSocket socket;
    socket.setBlocking(false);
    sf::Clock clock;
    sf::Time send_t;
    char data[] = "hello";
    sf::sleep(sf::milliseconds(1000));
    clock.restart();
    if (socket.send(data, 6, "127.0.0.1", 54000) != sf::Socket::Done) {
        std::cout << "Error sending data.\n";
        return -1;
    }
    send_t = clock.restart();
    std::cout << "Send time: " << send_t.asMicroseconds() << "us\n";  
    return 0;
}
And I get:
Send time: 4250 us ( = 4.25 ms)
(in average, +/-200)
My system is: Windows XP SP3, but the hardware is newer than XP :) (3 years old)

Another question:
When listening on more sockets, is there any difference or advantage between using blocking sockets and a socket selector, or checking non-blocking sockets in a loop manually?
I understand the tutorial supports the socket selector way, but it isn't a big difference, is it? Does it maybe depend on if you are using UDP or TCP?

Thank you for your help.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Time duration of udp_socket.send(...)
« Reply #1 on: September 04, 2013, 12:26:03 pm »
Measuring the time taken by a single function call is really a bad idea. You should use a profiler, or at least measure a significant amount of calls.

Quote
When listening on more sockets, is there any difference or advantage between using blocking sockets and a socket selector, or checking non-blocking sockets in a loop manually?
It really depends on what suits your design and requirements the best. For example, it depends if you have a game loop running, if you have a dedicated thread for network, etc.
Laurent Gomila - SFML developer

Vaclav

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Time duration of udp_socket.send(...)
« Reply #2 on: September 04, 2013, 01:19:53 pm »
Thank you Laurent.
You're right, only the first call takes longer.

 

anything