Search code examples
pythonperformancesocketsprogramming-languagesio

Python socket I/O performance compared to other languages


I'm writing a python program, to work on windows, the program has heavy threading and I/O, it heavily uses sockets in its I/O to send and receive data from remote locations, other than that, it has some string manipulation using regular expressions.

My question is: performance wise, is python the best programming language for such a program, compared to for example Java, or C#? Is there another language that would better fit the description above?


Solution

  • Your requirements are:

    1. to work on windows;
    2. the program has heavy threading and I/O
    3. it heavily uses sockets in its I/O to send and receive data
    4. it has some string manipulation using regular expressions.

    The reason it is hard to say definitively which is the best language for this task is that almost all languages match your requirements.

    1. Windows: all languages of notes
    2. Heavy use of threads: C#, Java, C, C++, Haskell, Scala, Clojure, Erlang. Processed-based threads or other work arounds: Ruby, Python, and other interpreted languages without true fine-grained concurrency.
    3. Sockets: all languages of note
    4. Regexes: all languages of note

    The most interesting constraint is the need to do massive concurrent IO. This means your bottleneck is going to be in context switching, cost of threads, and whether you can run thread pools on multiple cores. Depending on your scaling, you might want to use a compiled language, and one with lightweight threads, that can use multiple cores easily. That reduces the list to C++, Haskell, Erlang, Java, Scala. etc. You can probably work around the global interpreter lock in Python by using forked processes, it just won't be as fine grained.