Boosting Python Performance with Cython
What is Cython?
Cython is a compiler that translates Python code into C or C++ code. This allows developers to write Python-like code while achieving the performance benefits of a compiled language. It is not part of the Python standard library and must be installed separately using a package manager like pip.
By converting Python scripts into C or C++ code, Cython enables the creation of high-performance extensions. These extensions can be compiled into machine code, significantly accelerating execution speed. Cython provides features such as static type declarations and direct calls to C functions, which are key to its performance gains.
To install the Cython package, execute the following command in your terminal:
pip install Cython
How to Use Cython
Using Cython involves a few specific steps. Below is a basic example demonstrating the process of compiling a Python-like function into a C extension.
- Create a file named
my_module.pyxwith the following content. This file uses a Cython-specific syntax for type hints to enable optimization.
def calculate_sum(int x, int y):
return x + y
- In the same directory, create a
build_module.pyfile. This script uses thesetuptoolslibray to configure and build the Cython extension.
from setuptools import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("my_module.pyx")
)
- Navigate to the directory containing both files and run the build command. This will generate the C source file and compile it into a shared object (
.so) file.
python build_module.py build_ext --inplace
After running the command, you will find a new my_module.c file (the generated C code) and a my_module.so file (the compiled shared library) in your directory.
You can now verify the compiled module in an interactive Python session:
Python 3.9.7 (default, Sep 16 2021, 08:50:36)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import my_module
>>> my_module.calculate_sum(50, 75)
125
>>>
For more control, you can manually compile the generated C file. First, compile it into an object file, then link it into a shared library:
gcc -fPIC -c my_module.c -o my_module.o -I/usr/include/python3.9/ -lpython3.9
gcc -shared -o my_module.so my_module.o
This process demonstrates Cython's core strength: providing the development speed of Python with the execution speed of C. The resulting .so file acts as a native Python extension, allowing you to call high-performance C code from your Python programs.
Understanding .pyx Files
A file with the .pyx extension is a Cython source file. It contains code written in a superset of the Python language, which Cython can translate into C or C++ for compilation. These files allow developers to write code that is mostly Python-like but can include static type declarations and direct C API calls to maximize performance.