Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take two weeks Trial! Try Now

How to make Python faster using Cython?

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.

python-faster-using-cython

We will see the above flow diagram in working with the help of the example below.

The Example

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.

1. def sum_even_no(x): 2. y = 0 3. for i in range(x): 4. if i%2 == 0: 5. y += i 6. return y

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.

1. def sum_even_no(x): 2. cdef int y = 0 3. cdef int i 4. for i in range(x): 5. if i%2 == 0: 6. y += i 7. return y

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:

  1. def - They are regular python function, used in Python only.
  2. cdef - They are only Cython functions, for this, there is no C translation of Python.
  3. 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)

  1. from distutils.core import setup
  2. from Cython.Build import cythonize
  3. setup(
  4. 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.

Command -:

“Python setup.py build_ext --inplace”

It will build and show such type of result -:

python-faster-using-cython

After executing this command, you can check your folder, and it will create two new files a follows.

python-faster-using-cython

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)

1. import timeit 2. ## It will store the time taken by test_cy(Cython code) file with repitions 1000. 3. cy = timeit.timeit('''test_cy.sum_even_no(500)''',setup = 'import test_cy', number=1000) 4. ## It will store the time taken by test_py(Python code) file with repitions 1000. 5. py = timeit.timeit('''test_py.sum_even_no(500)''',setup = 'import test_py', number=1000) 6. print('Cython is {}x faster'.format(py/cy))

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.

Conclusion

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.

Recent Blogs

Categories

NSS Note

Some of our clients

team