3 ms·
Actually, no, it's worse than that. Threaded code on Python often runs slower on multiple CPUs than on a single CPU. David Beazley ran some numbers on a simpl
by nostrademons 16y ago
Actually, no, it's worse than that. Threaded code on Python often runs slower on multiple CPUs than on a single CPU. David Beazley ran some numbers on a simple benchmark than indicated that running the same program with two threads on two CPUs was twice as slow than running the same program on a single thread on one CPU. It's not just a matter of not being able to use extra cores: the GIL actively slows down the other threads through context-switching overhead:
http://blip.tv/file/2232410 http://blip.tv/file/2232410
Also, the serialization introduced by the GIL hurts worst when you're gluing together complex applications. Say that you have a finely-tuned C++ app that spends about 20% of its time in an embedded Python interpreter (number chosen to make the math easy). Now you make the C++ multithreaded and run it on four cores. The 80% of the CPU time spent in C++ gets parallelized effectively, so now it's only 20% of the original runtime. But the Python access is all serialized by the GIL, so it's still 20% of the original time, or 50% of the new runtime. At this point, you're highly likely to get thread contention issues between different threads attempting to acquire the GIL (as per the video), so your Python performance goes down even more - which makes thread contention even more likely, and so on.
By contrast, if you'd rewritten the Python in Haskell, the most you could get was a 25% speedup, because most of the execution time was spent in C++ anyway.
- jrockway 16y agoYeah; I've read that many implementations of GIL-less Python make it significantly slower overall. Slow on one core, slow on four cores. Great. With repsect to your second example, I would argue that if you're gluing, you put the glue on the outside of the core -- hence, your single-threaded Python wrapper would be running your fast, multi-threaded Haskell or C++. In that case, the only pain point is supplying data and interpreting the results, which is usually well inside the realm of Perl/Python/Ruby's capabilities.
- nostrademons 16y agoMost large-size mixed Python/C++ systems have multiple layers: http://c2.com/cgi/wiki?AlternateHardAndSoftLayers http://c2.com/cgi/wiki?AlternateHardAndSoftLayers "Inside" or "outside" of the core doesn't have much meaning in this context. Typically, you have a C++ driver that provides your application's main(), which invokes the Python interpreter, which can then call back into C libraries. Or you might wrap that itself with a Python script. That's the approach that the Tornado web server takes: a Python driver program, which enters an epoll select loop, which calls back into the Python to handle requests, which may themselves call functions written in C. The point is that it's not just Python scripts calling super-fast C libraries, it's also often large C++ apps invoking Python scripts for lesser-used features.