Optimizing Python with Cython

Python is a widely used programming language nowadays, among other reasons because it is comfortable to program at a high level, it has many libraries and it is an easily maintainable code. Despite all these advantages, it should be noted that it is notoriously slow, so it is convenient to know some tools that allow you to speed up Python code.

Cython vs Python

One of the ways in which Python can be sped up is by combining it with Cython code. To illustrate this, we will take as an example the calculation of the Pi number using the Monte Carlo method (example taken from the Numba webpage, another way to speed up Python).

Speed up Python
Speed up Python

It is a very simple code, in which the number Pi is calculated by sampling a random number of points and using the ratio between the area of ​​a square and a circle. The area of ​​the circle is estimated as the number of points within the circle, and that of the square as the total number of points.

Montecarlo
Montecarlo

Using 100,000 points, the Python function takes 38.5ms, while the Cython one takes 2.98ms, which is 12.92x times faster.

What is Cython?

Cython is a language that is written in a very similar way to Python, but it allows the use of C-type libraries and variables. Therefore, the speed of C can be achieved while maintaining the simplicity of syntax provided by Python. In addition, one of its great versatility is that explicit definitions of C variables can be mixed with Python variables, such as dictionaries.

However, programming in Cython also has a number of drawbacks, such as the need to compile the code separately before running the program. There is also the fact that compared to languages ​​like C or Fortran, it doesn’t have as good support for memory parallelization.

How to use Cython?

Three files are required for a Cython program to work. The first and most important is the one that contains all the Cython code, with a .pyx extension. In the cython example of the calculation of the number pi, you can see how within this code you can access C libraries, as well as declare the variables with their type to increase speed.

It is also important to highlight the declaration of the functions, which can be declared in three different ways: def, cpdef and cdef.

The first indicates that the function can only be accessed from a Python function, the second that it can be accessed from Python and from Cython, and the last that it is only accessible from Cython.

The second of the required files is the setup.py, which generates the Cython extension when compiled.

And, finally, the third script is the Python script, which calls the file generated by setup.py to be able to import the Cython code as if it were a library, and thus be able to access its functions as it would be done with any other.

(Image by DavidBrooksPokorny – Own work, CC BY-SA 3.0, wikimedia)

Cython files
Cython files

In this way, for particularly slow parts of the code such as very long loops where Numpy cannot be used or for mathematical computations, Cython can help speed up the execution of the program.

 

We hope this article has been useful to you. If you have an engineering project in your hands and you think we can help you, here is the link where you can contact us and explain more about it.