Cython and Numba

Writing code in python is easy: because it is dynamically typed, we don’t have to worry to much about declaring variable types (e.g. integers vs. floating point numbers). Also, it is interpreted, rather than compiled. Taken together, this means that we can avoid a lot of the boiler-plate that makes compiled, statically typed languages hard to read. However, this incurs a major drawback: performance for some operations can be quite slow.

Whenever possible, the numpy array representation is helpful in saving time. But not all operations can be vectorized.

Cython is a technology that allows us to easily bridge between python, and the underlying C representations. The main purpose of the library is to take code that is written in python, and, provided some additional amount of (mostly type) information, compile it to C, compile the C code, and bundle the C objects into python extensions that can then be imported directly into python.

These materials are based partially on a tutorial given by Kurt Smith (Enthought) at the Scipy conference, 2013.

The template we use for these lesson websites is based on the lesson template used in Data Carpentry and Software Carpentry workshops,


17:00 Introduction Why use Cython?
How do you install Cython?
What are some ways you can use Cython?
17:15 Compiling Cython code How do we compile Cython code in a typical project?
How do we create Cython objects with no Python overhead
17:25 Using annotations to improve performance even more How do we diagnose performance bottlenecks?
How can we improve these bottlenecks even more?
17:40 Numba What other options do we have to speed up Python code?
17:55 Wrap-Up What have we learned?
17:57 Finish