High-Performance Python – Compiled Code and C Interface

Summary

Python is amazingly popular right now, with thousands of modules that can be used to extend its capability. However, in the high-performance world, Python is not known for being fast. A number of people have written tools and extensions to make Python faster.

In this article, I presented Numba, which compiles Python code with a just-in-time compiler invoked with a decorator. A simple summation example was found to be much faster than the original Python code. More on Numba will be presented in future articles.

Cython was also discussed briefly. It is a tool for “translating” Python code into C code. A simple Python “makefile” translates and compiles the code for you. It’s actually pretty simple to use and allows you to create a large Python module with lots of functions.

Finally, I presented ctypes, which uses an approach opposite that of Numba and Cython by taking existing C code that is compiled into a library, coupled with some ctypes functions and variables, to create a module that can be used in Python. The ctypes library has a great deal of flexibility, so you can incorporate code written in C into Python.