Optimization of Scientific Code with Cython: Ising Model

Before I get to the videos, I wanted to say a few words about when and why you might choose Cython.

With scientific Python code, before turning to Cython I’d suggest going as far as you can with vectorization. Vectorization involves the judicious use of built-in routines in NumPy, SciPy, Pandas, and other libraries to reduce the number of explicit for-loops in your code. It can work quite well in many situations, and doesn’t require any sort of special compilation step in running your code. See my PyCon 2015 talk, Losing Your Loops for an intro to this approach.

When a problem cannot be easily solved using standard vectorization approaches, Cython is a good choice. Cython provides a bridge between Python and C code, and is quite mature and reliable: it forms the basis of much of the PyData/Scientific Python ecosystem (for example, Cython is used heavily in NumPy, SciPy, Pandas, Scikit-Learn, and many other packages).

Other approaches to optimizing Python code are available, but I tend not to use them as often as Cython:

The PyPy project is an alternative implementation of Python that avoids the slow loops of the default CPython implementation. Although it is quite promising, it currently does not support many of the core scientific packages, so is not the best choice for scientific code (though that has been changing).

Numba is a Python-to-LLVM converter which can often give you 100x speedups in Python by adding a simple compilation decorator to Python functions. For an example of Numba being used on code like that in this notebook, see Matthew Rocklin’s post. Though it is convenient, Numba doesn’t support all Python constructs yet, and can be difficult to optimize when the most straightforward approach fails.