In this tutorial, we will discuss how to make Python run faster using Cython. First, we will take the general idea of Cython i.e., How it is working? How to install Cython? Further, we will look through an example of how to take advantage of it with little modifications in your code. Let’s get started to deep dive into the details.
Why do we need Cython?
Cython acts as an intermediary between Python and C/C++. Why do we need to make Python faster, as it is considered one of the advanced languages? We also prefer using Python due to its simplicity in writing code, but still, it is lagging in computations. It is the core heart reason for using the Cython version in Python.
How to install Cython?
You have to run a simple command on the command prompt. One of the simplest ways of installing Cython is through pip:
Pip install Cython
For more details refer to -: https://cython.readthedocs.io/en/latest/src/quickstart/install.html
What is Cython?
Cython allows us to add typing i.e., to add the data type to variable and reference class attributes, and after that, it will generate a C Source file of the Cython file. And as well as a c extension for Python. Let’s refer to the below diagram to get an idea of how Cython works.
We will see the above flow diagram in working with the help of the example below.
To keep this blog simple, I will take a simple example to show the computation difference between Python and Cython.
Calculating the sum of even no.s in a list
This code example will calculate the sum of even no.s. In this code, we need to use for loop, and in the upcoming session, we will see how Cython makes run for loop efficiently.
Simple Python Code (test_py.py)
Suppose the name of the file is test_py.py. Here we are writing simple Python code only.
Cython Code (test_cy.pyx)
Now, we will write the Cython code for the same logic above. You must save the Cython file as a .pyx extension.
In the above code, you can see that we did not do many changes instead of adding datatypes in front of the ‘y’ and ‘i’ variables. Some of the typing declarations are as follows:
- def - They are regular python function, used in Python only.
- cdef - They are only Cython functions, for this, there is no C translation of Python.
- cpdef - It will create a C function and a wrapper for Python. We'll be mostly using cpdef.
Building the extension module
There are a few other methods to make this Cython executable, but the standard is to build a Python extension. These methods avoid dynamic compile on every run. And for this, we need to add a setup.py file and build every time whenever there are certain changes in Cython code.
After building this setup.py file, you are ready to import the above code (Cython) as a module and free to use its functions.
Let’s create a setup file (setup.py)
- from distutils.core import setup
- from Cython.Build import cythonize
- ext_modules = cythonize(["test_cy.pyx"]))
We need to create it after creating a setup.py file. And using the command line, we will build it. First, locate the folder where all these files are created and then open cmd.
Run this command to build the setup.py file to generate a c source and Python extension file for the Cython code.
“Python setup.py build_ext --inplace”
It will build and show such type of result -:
After executing this command, you can check your folder, and it will create two new files a follows.
I hope all is clear so far. Now, you are ready to import it as a module. Now will create a new python file (checking_module.py) and see the difference between the computation time of Python and Cython.
Let’s create this file (checking_module.py)
Output of above code:
Cython is 18.432300452810082x faster
Every time you will run this code will give different output.
In the code above, we are calculating the time taken by the same Cython code and Python Code. For that, we are using the time it module of Python, and with 1000 repetitions as for a single run, it will give very little value and will not be able to determine the difference. You can also check by changing the parameter values, such as the value passed in the sum_even_no function and the number of repetitions.
In this blog, we have seen how to make Python faster using Cython, As Cython is the C version of Python development services. You should also check with the help of code examples that how much Cython is fast. Here, in our code examples, it was 18 times faster than before. In some scenarios, we have to increase the speed, such as some machine learning libraries Numpy. And also, pandas are using an efficient version of Python that uses Cython. I hope you get a better understanding of what Cython is and how to use it.