Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyPy vs. Nuitka

Tags:

pypy

nuitka

During the last days, I was toying around with Nuitka, a tool that compiles Python into executable C/C++ programs.

I have not found any speed advantage of Nuitka (compared to PyPy). What is the meaning of Nuitka, then? Am I missing something?

like image 590
user258532 Avatar asked Dec 27 '17 12:12

user258532


People also ask

Does Nuitka improve performance?

Python versions 2.6, 2.7, and 3.3 through 3.7 are all supported, including constructions like async . Python programs compiled with Nuitka can benefit from large performance boosts. Hayen claims a Nuitka-compiled version of the Pystone benchmark runs some 312 percent faster than a conventional CPython implementation.

Does PyPy support Python 3?

PyPy comes in two versions: one is fully compatible with Python 2.7; the other is fully compatible with one 3. x version.

How does Nuitka work?

Nuitka (pronounced as /njuːtkʌ/) is a source-to-source compiler which compiles Python code to C source code, applying some compile-time optimizations in the process such as constant folding and propagation, built-in call prediction, type inference, and conditional statement execution.

What is the difference between CPython and Cython?

CPython is the implementation of the language called “Python” in C. Python is an interpreted programming language. Hence, Python programmers need interpreters to convert Python code into machine code. Whereas Cython is a compiled programming language.


2 Answers

Nuitka and PyPy have very different goals.

Nuitka does Ahead Of Time (AOT) compilation of your python project to C, using the python C-API. In this way it is more similar to Cython. It is still a young project, but impressively has achieved full compatibility with the enormous python language spec. The next step will be to enable optimizations in the compiler process, much like gcc -O3. Note that Nuitka is used to transform your python code into an executable. You then "ship" the executable, with some level of obscifucation of the original python code.

PyPy does Just In Time (JIT) compilation of running code to assembly. It traces your running code, identifies hot spots, and produces faster versions of the hot sections of your program. It too, has full compatibility with the python language spec. It does not transform your python code ahead of time, so you "ship" your python code as the final product.

I expect both projects will continue to improve speed of execution, but they target very different needs.

like image 140
mattip Avatar answered Oct 02 '22 16:10

mattip


Nuitka: Nuitka is written in Python itself, it takes Python module as input and provides c program as an output. The output is executed against libpython and other static c files and works as an extension module or an executable.

It is important to know that the Nuitka compiled output is highly optimized and faster than the raw python program, but it still doesn’t match the performance of executable created from a pure C code. Many developers claim to have gained 4x speed with Nuitka compiled programs when compared with basic Python code interpretation.

The best part of Nuitka is that it is compatible with almost all versions of Python including 3.3, 3.9, 2.6, 2.7, and more.

It is also very actively developed as of now and developers aim to transform Nuitka into a compiler that can provide native C performance out of the Python code.

PyPy: Like Nuitka, PyPy also supports both Python 2 and Python 3 specifications. PyPy is the most popular alternative to CPython which is the default Python compiler.

PyPy compiler was originally created to speed up Python execution and to do so, it utilizes Just in Time compilation (JIT). JIT-based compilers take raw code as input and turn the code to machine code prior to execution.

Execution speed is not the only advantage you get with PyPy, it also reduces memory usage and provides an option to write stackless applications as stackless python does.

It is also important to note that PyPy doesn’t give you a performance or memory utilization advantage on short-running processes. However, the performance gains are significantly noticeable when you have long-running processes.

The lack of performance with short running processes is mainly because JIT compilers take time to warm up and hence bring in inherent initialization overheads.

The same is true for memory usage as well, with small programs, the additional memory required by JIT overweighs any benefits gained from the execution of compiled code. Developers have experimented with PyPy to gain up to 15x performance when compared to raw python code execution.

More details can be found here at Nuitka vs PyPy

like image 37
sam disilva Avatar answered Oct 02 '22 16:10

sam disilva