30. Cython

Audience level:
March 11th 8:25 a.m. – 8:30 a.m.


The Cython language is very close to the Python language, but Cython additionally supports calling C functions and declaring C types on variables and class attributes. The result is a natively compiled module that can be loaded into the regular Python interpreter. Cython can be used both to eliminate bottlenecks and for conveniently wrapping C/C++ libraries.


Writing Cython code feels like being in Python and C at the same time, mixing the possibilities of both languages. There are two primary usecases for doing this: Speeding up code, and calling C/C++ libraries.

1. Speeding up code: Cython can give a small speed boost to most pure Python code, at the cost of an extra compilation step. With a little bit of extra annotation on top of your Python code, Cython can generate essentially pure C code, which, in the right situations, can give speedups in the range of 5 to 20 times faster than Python. In CPU-heavy number crunching code, Cython can be hundreds of times faster than Python.

Optimizing all your code makes no sense. Since Cython is a superset of Python, it is convenient to first implement everything in Python, then use profiling and add Cython annotations only at the proven bottlenecks. Good Cython programmers use Cython sparingly, but with great effect.

While everybody would love to have Python to run as fast as C without any manual typing, Cython represents proven and pragmatic technology that works well right now.

2. Binding to C/C++ code: Many Python library bindings are written in Cython. Unlike projects like SWIG, Cython does not encourage bringing the C or C++ API one-to-one into Python, but encourages the creation of a nice Pythonic interface. Unlike ctypes, it is easy to create fully portable wrappers that never segfaults (although ctypes has the advantage of not requiring compilation).

Some examples of Cython wrappers are the ZeroMQ Python bindings (pyzmq) and the lxml project. Within scientific programming, the use of Cython to interface with the large number of existing C/C++/Fortran libraries is quickly becoming the norm rather than the exception.