Getting Started with Cython: Boosting Python Performance

Hello CythonPython is a powerful and versatile programming language, but it can sometimes fall short in terms of performance, especially when dealing with computationally intensive tasks.This is where Cython comes in. Cython is a superset of Python des…


This content originally appeared on Level Up Coding - Medium and was authored by Rahul Beniwal

Hello Cython

Python is a powerful and versatile programming language, but it can sometimes fall short in terms of performance, especially when dealing with computationally intensive tasks.

This is where Cython comes in. Cython is a superset of Python designed to give C-like performance with code that is written mostly in Python, allowing you to write Python code that calls back and forth from and to C or C++ code natively.

What is Cython?

Cython is a programming language that makes writing C extensions for Python as easy as Python itself. It is a superset of Python, meaning that any Python code is also valid Cython code. Cython allows you to:

  • Write Python code with C-like performance.
  • Call C functions and declare C types on variables and class attributes.
  • Compile Python code to C to gain significant performance improvements.
  • Allow to do real multithreading as Code will be executing outside the GIL

Let’s start

Step 1 -> Installing Cython

You can install it from pip.

pip install cython

Step2 -> Writing First Cython Code

Let’s first write a function to calculate the sum of squares.

import time

def sum_of_squares(n):
result = 0
for i in range(n):
result += i * i
return result

n = 1_000_000_00
start = time.time()

sum_of_squares(n)
end = time.time()

print(f"took {end - start} seconds.")
took 3.813304901123047 seconds.

Let’s convert this to Cython code. It would not take much effort to do it.

The filename should have pyx as file extension as cython is extension to native Python.

# file.pyx

import time
def sum_of_squares(n):
start = time.time()
cdef int result = 0
cdef int i
for i in range(n):
result += i * i
end = time.time()

print(f"took {end - start} seconds.")
return result

Notice the changes

  • We specified the type of input parameter n as int.
  • We declared the variable result and the loop variable i as int using the cdef keyword.

Compiling Cython Code

To compile the Cython code, you need a setup.py file.

from setuptools import setup
from Cython.Build import cythonize

setup(
name="Hello World",
ext_modules=cythonize("file.pyx"),
)

Then, run the following command to build the extension module:

python setup.py build_ext --inplace

This command generates a shared object file (.so on Unix-like systems or .pyd on Windows) that can be imported directly into Python.

.
├── build
│ ├── lib.linux-x86_64-cpython-310
│ │ └── file.cpython-310-x86_64-linux-gnu.so
│ └── temp.linux-x86_64-cpython-310
│ └── file.o
├── cfile.py
├── file.c
├── file.cpython-310-x86_64-linux-gnu.so
├── file.pyx
└── setup.py

Using the Compiled Cython Code

You can now use the compiled Cython function in your Python code. cfile.py

from file import sum_of_squares

print(sum_of_squares(1_000_000_00))
took 0.02080059051513672 seconds.
-1452071552

😆 c Does not have abstraction like Python does to represent huge huge numbers. Here the sum of squares is so large that is overflow the range of the maximum number int can store. But it took only 0.02 to complete the above operation pretty fast.

Let’s use unsigned long it to store the result.

import time
def sum_of_squares(n):
start = time.time()
cdef unsigned long result = 0
cdef unsigned long i
for i in range(n):
result += i * i
end = time.time()

print(f"took {end - start} seconds.")
return result

we need to recompile it and let’s run this.

took 0.0002522468566894531 seconds.
333332833333500000

I found it around 130 times faster.

Advanced Cython Features

  • Static Typing: By adding type declarations, you can achieve further performance improvements.
  • Calling C Functions: You can call external C functions directly from Cython code.
  • Parallelism: Cython supports parallel processing with the prange function from the cython.parallel module.

Popular Use Cases

Cython is the backbone of some of the popular existing libraries including

  • NumPy
  • SciPy
  • Pandas
  • scikit-learn
  • lxml
  • PyTables

and many more.

Conclusion

Cython is a powerful tool for optimizing Python code, making it an excellent choice for performance-critical applications. By allowing you to write Python code with C-like performance, Cython provides the best of both worlds: the simplicity and readability of Python and the speed of C. Whether you’re working on numerical computations, data processing, or integrating with C/C++ libraries, Cython can help you achieve significant performance gains.

I am planning to explore cython in more depth. If you found this helpful then you can follow Rahul Beniwal and leave a 👏 to encourage.

You can also check similar articles by me.


Getting Started with Cython: Boosting Python Performance was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Rahul Beniwal


Print Share Comment Cite Upload Translate Updates
APA

Rahul Beniwal | Sciencx (2024-08-01T13:24:07+00:00) Getting Started with Cython: Boosting Python Performance. Retrieved from https://www.scien.cx/2024/08/01/getting-started-with-cython-boosting-python-performance/

MLA
" » Getting Started with Cython: Boosting Python Performance." Rahul Beniwal | Sciencx - Thursday August 1, 2024, https://www.scien.cx/2024/08/01/getting-started-with-cython-boosting-python-performance/
HARVARD
Rahul Beniwal | Sciencx Thursday August 1, 2024 » Getting Started with Cython: Boosting Python Performance., viewed ,<https://www.scien.cx/2024/08/01/getting-started-with-cython-boosting-python-performance/>
VANCOUVER
Rahul Beniwal | Sciencx - » Getting Started with Cython: Boosting Python Performance. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/01/getting-started-with-cython-boosting-python-performance/
CHICAGO
" » Getting Started with Cython: Boosting Python Performance." Rahul Beniwal | Sciencx - Accessed . https://www.scien.cx/2024/08/01/getting-started-with-cython-boosting-python-performance/
IEEE
" » Getting Started with Cython: Boosting Python Performance." Rahul Beniwal | Sciencx [Online]. Available: https://www.scien.cx/2024/08/01/getting-started-with-cython-boosting-python-performance/. [Accessed: ]
rf:citation
» Getting Started with Cython: Boosting Python Performance | Rahul Beniwal | Sciencx | https://www.scien.cx/2024/08/01/getting-started-with-cython-boosting-python-performance/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.