C Python Network Performance

GM M gmm2064 at yahoo.com
Wed Jul 11 16:36:39 EDT 2007


Hello,

I am writing an application bulk of which is sending and receving UDP data. I was evaluating which language will be a better fit for the job. I wrote following small pieces of code in Python and C respectively:

from socket import *
import sys

if __name__ == '__main__':
    s = socket(AF_INET, SOCK_DGRAM)
    while True:
        s.sendto("hello", (sys.argv[1], int(sys.argv[2])))


#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>

#define true 1
#define false 0

int main(int len, char** args){
  int s = socket(AF_INET, SOCK_DGRAM, 0);
  struct sockaddr_in s_addr;
  inet_pton(AF_INET, args[1], &(s_addr.sin_addr));
  printf("%s:%s\n", args[1], args[2]);
  in_port_t port;
  sscanf(args[2], "%u", &port);
  s_addr.sin_port = htons(port);
  s_addr.sin_family = AF_INET;
  char* msg = "hello";
  int msg_len = 6;
  int addr_len = sizeof(struct sockaddr);
  struct sockaddr* s_g_addr = (struct sockaddr*) &s_addr;
  while(true){
    int success = sendto(s, msg, msg_len, 0, s_g_addr, addr_len);

  }
}

Both pieces of code, take IP address and port on command line initely send UDP messages to the target. In this experiment target is another machine of mine and I record the data rate received at that machine. I was surpirised to see that python code was able to achieve 4-10 times higher data rate than C. I was expecting C code to perform the same, if not better.

Can anyone please explain why this is the case?

Thanks

Ghulam Memon

       
---------------------------------
Building a website is a piece of cake. 
Yahoo! Small Business gives you all the tools to get online.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070711/6e83ab91/attachment.html>


More information about the Python-list mailing list