Perspective about JITs in python: numba jumba

Rajdeep Biswas
4 min readJun 24, 2019
Edited from File:Python-unicorn.svg From Wikimedia Commons, the free media repository

Standard implementations of python today translate the source code into byte code and then the byte code is parsed and interpreted during execution. Since this byte code is generally not compiled as a machine level code a similarly written Python program can perform slower than a low-level compiled language like C. In terms prominent alternatives we have [1],

· Cython [2]: Cython is an optimizing static compiler for both the Python programming language and the extended Cython programming language (based on Pyrex). It makes writing C extensions for Python as easy as Python itself.

· PyPy [3] : The PyPy system is another standard CPython re-implementation, focused on performance.It provides a fast Python implementation with a JIT (just-in-time) compiler, provides tools for a “sandbox” model that can run untrusted code in a secure environment.

· Jython [4]: The Jython system (originally known as JPython) is an alternative implementation of the Python language, targeted for integration with the Java programming language.

· IronPython [5]: A third implementation of Python, and newer than both CPython and Jython, IronPython is designed to allow Python programs to integrate with applications coded to work with Microsoft’s .NET Framework for Windows, as well as the Mono open source equivalent for Linux.

· Stackless [6]: The Stackless Python system is an enhanced version and re-implementation of the standard CPython language oriented toward concurrency.

As you can imagine all these are separate implementations of Python and not a package. Enter numba [7] (pip install numba) which is an open source JIT compiler that translates a subset of Python and NumPy code into fast machine code. When a Numba function is called for the first time, the function is compiled into machine code (for the given argument types) and stored. Subsequent calls with the same argument types use the machine code and are therefore very fast. Because the compilation happens at run time, the first function call can be slower than usual. Numba-compiled numerical algorithms in Python can approach the speeds of C or FORTRAN. We do not need to replace the Python interpreter, run a separate compilation step, or even have a C/C++ compiler installed. Just apply one of the Numba decorators to your Python function, and Numba does the rest. Numba also works great with Jupyter notebooks for interactive computing, and with distributed execution frameworks, like Dask and Spark. Numba offers a range of options for parallelizing your code for CPUs and GPUs, often with only minor code changes.

I did some basic tests which demonstrates the power as well as caveats for JIT:

Generally speaking, single execution low latency process will not perform well in JIT because the first-time compilation takes time.

Demonstrated below:

Without using numba (just vanilla python 3.6.6):

no_numba_no_loop

Using numba JIT:

numba_no_loop

As you can see from the above screenshots numba enabled JIT performs greater than 500X worse in this case.

Now let us loop the same function 50,000 times and see the results.

Without using numba (just vanilla python 3.6.6):

no_numba_50k_loop

Using numba JIT:

numba_50k_loop

With numba the program ran greater than 30X faster. 0.44.1 is the numba version that is getting printed out. You will also find more performance comparisons here [8]

The source code for this tests and few more can be found in my git repo https://github.com/RajdeepBiswas/jumba_numba/tree/master/source_code

Remember, your results will vary but for large loops used in this example and in many scientific implementations numba will be faster by manifolds.

References

[1]

M. Lutz, Learning Python, O’Reilly Media. Kindle Edition.

[2]

“cython,” [Online]. Available: https://cython.org/.

[3]

“pypy,” [Online]. Available: https://pypy.org/index.html.

[4]

“jython,” [Online]. Available: https://www.jython.org/.

[5]

“ironpython,” [Online]. Available: https://ironpython.net/.

[6]

“stackless,” [Online]. Available: https://github.com/stackless-dev/stackless/wiki.

[7]

“numba,” [Online]. Available: https://numba.pydata.org/.

[8]

“benchmark,” [Online]. Available: https://pybenchmarks.org/u64q/benchmark.php?test=all&lang=numba&lang2=pypy&data=u64q.

--

--

Rajdeep Biswas

Leader Data & AI - Manufacturing. Leading an organization focused on enabling Digitally Transformative solutions through Data & AI.