Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are JITted Python implementations still slow?

I understand why interpretation overhead is expensive, but why are JITted Python implementations (Psyco and PyPy) still so much slower than other JITted languages like C# and Java?

Edit: I also understand that everything is an object, dynamic typing is costly, etc. However, for functions where types can be inferred, I'm not sure why this matters.

like image 640
dsimcha Avatar asked Dec 21 '10 14:12

dsimcha


People also ask

Why is Python used despite being slow?

Because those libraries are written in C++, when using them in Python scripts, we're using C++ in the background. That makes the development process fast because of Python, and makes our script as fast as those written in C++. The same is true for image preprocessing (OpenCV library).

Why are Python programs slow?

Unlike other popular programming languages including C# or JAVA, Python is dynamically typed and an interpreted language. It is slow primarily due to its dynamic nature and versatility.

What is the fastest Python implementation?

The fastest implementation of python is pypy. As mentioned above, pypy uses justin-time compilation. The JIT compilation makes pypy faster than the other implementations. JIT compilation lets the source code to be compiled into native machine code which makes it very fast.

Why is Python so slow compared to C++?

Internally Python code is interpreted during run time rather than being compiled to native code hence it is a bit slower. Running of Python script v/s running of C/C++ code: Python: First it is compiled into Byte Code. This Byte Code is then interpreted and executed by the PVM (Python Virtual Machine).


2 Answers

The simplest possible answer is that PyPy is simply not yet as fast as hotspot and Psyco never will.

Writing a reasonable JIT is a long and tedious process and it took for example many years for hotspot to get where it is (with a lot of funding as well). The more complex and dynamic the language is, the longer it takes. On the bright side, we have good examples how JITs for dynamic languages can be very fast, take LuaJIT for one, which can beat C or JVM on many examples.

There are good news however: According to speed center PyPy got 27% faster on average in the past 100 revisions, so it'll happen eventually.

like image 89
fijal Avatar answered Sep 23 '22 05:09

fijal


People have already pointed out the technical details, so I'll add another factor: money.

In the last few years, Javascript VMs (Google's V8, Mozilla's Tracemonkey & Jaegermonkey, Apple's Nitro) have delivered a huge speed increase for another dynamic language. That's been driven in large part by Google's desire to make web apps more powerful. Python just doesn't have a big company standing to gain by making it 50x faster.

Oh, and the integration with C extensions like numpy means that speed is rarely critical for Python code, anyway.

like image 21
Thomas K Avatar answered Sep 22 '22 05:09

Thomas K