Skip to content

[Linux] Speed Up Pure Python Programs with Cython

When we developing a large-scale Python project, we will start to try various acceleration methods.

Generally, multi-processing and Cython are good choices for us. Today, I want to record the experience of using Cython. currently Cython is relatively easy to use on Linux, I heard that the configuration on Windows will be a bit of troublesome.

If you want to read more document of Cython, you can refer the following website: https://cython.readthedocs.io/en/latest/src/tutorial/cython_tutorial.html


What is Cython

Python is a easy-to-use programming language, but it has the disadvantage of slow execute speed. Although the execution speed of C language is very fast, it has the problem of lengthy code. (compared to Python)

Cython can make up for this shortcoming of Python. It can modify and compile the Python syntax into C language, and then executed.

So we can use Python to develop program and we have the execution speed like C Programming language.


How to Use Cython

First, we use Python to program a Fibonacci program.

import sys


def fibo(n):
    if n == 0: return 0
    elif n == 1: return 1
    return fibo(n-1) + fibo(n-2)

if __name__ == '__main__':
    print(fibo(int(sys.argv[1])))



And we named it fibo.py and execute it:

time python3 fibo.py 40

Output:

102334155 
real    0m34.126s
user    0m33.875s
sys     0m0.031s

As we can see, it took us about 34s.


Then we copy the file into a .pyx file.

cp fibo.py fibopyx


Install Cython:

sudo pip3 install cython


After installation, create a setup.py program to try to extend the Python program;

from distutils.core import setup
from Cython.Build import cythonize


setup(
    ext_modules = cythonize('fibo.pyx')
)



Use the following command to execute:

python3 setup.py build_ext --inplace


After execution, we will see that there are more files such as build folder:

  • fibo.c
  • fibo.cpython
  • fibo.cpython-35m-x86_64-linux-gnu.so


At this time, we will write another Python file to call the module. I named it fibo_cython.py.

import sys
import fibo


print(fibo.fibo(int(sys.argv[1])))



Then execute the code that already uses Cython this time:

time python3 fibo_cython.py 40

Output:

102334155 
real    0m19.115s
user    0m19.031s
sys     0m0.016s

It spent 19s! The speed is obviously improved.

Leave a Reply