udp sockets with python

M

Mag Gam

Hello,

When measuring round trip time for the UDP echo client/server the C
version is much faster. I was wondering if there is anything I can do
to speed up.

My current code for client looks like this....

sock=socket(AF_INET,SOCK_DGRAM)
for x in range (1000):
sock.sendto("foo",(server,port))
a=sock.recv(256)

sock.close()


The server code looks like this:
UDPsock=socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
while 1:
m,c=UDPsock,recvfrom(1024)
UDPsock.sendto('bar',c)

UDPsock.close()


I am measuring the round trip time using tcpdump. The C version is
giving me around 80 microseconds (average) and the python is giving me
close to 300 microseconds (average).
 
J

Jean-Paul Calderone

Hello,

When measuring round trip time for the UDP echo client/server the C
version is much faster. I was wondering if there is anything I can do
to speed up.

My current code for client looks like this....

sock=socket(AF_INET,SOCK_DGRAM)
for x in range (1000):
  sock.sendto("foo",(server,port))
  a=sock.recv(256)

sock.close()

The server code looks like this:
UDPsock=socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
while 1:
  m,c=UDPsock,recvfrom(1024)
  UDPsock.sendto('bar',c)

UDPsock.close()

I am measuring the round trip time using tcpdump. The C version is
giving me around 80 microseconds (average) and the python is giving me
close to 300 microseconds (average).

Try replacing the hostname in your send calls with an IP address. If
you're not passing an IP address here, then the Python version has to
do a name lookup for each send, I bet your C version is not.

Jean-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top