DEV Community

Cover image for Get more than 30X speedup on your Python code
Rizwan Hasan for TechLearners

Posted on • Edited on

Get more than 30X speedup on your Python code

Python is the world's best programming language. But wait for a minute. Really? Some people will say in the comment that, "Bro, It's so slower than others in the market". By the way, I'm not going to argue with anyone about this best programming language selection war. But those who become upset after having a defeat from the war only based on the point of Python is slow, I'm here to give them a push. So, let's jump into the mission of making Python 30X faster.

Cython Logo

What is Cython?

Cython is an optimizing static compiler for both the Python programming language and the extended Cython programming language based on well-known Pyrex. It makes writing C extensions for Python as easy as Python itself. In other words, Cython is an intermediate step between Python and C/C++. It allows writing pure Python code with some minor modifications, which translates into the C code directly.

How Cython boosts?

Cython aims to become a superset of Python which gives it high-level, object-oriented, functional, and dynamic programming features. Source codes written in Python gets translated into optimized C/C++ code. This code is executed within the CPython runtime environment, but at the speed of compiled C and with the ability to call directly into C libraries. At the same time, it keeps the original interface of the Python source code, which makes it directly usable from Python code. Thus it compiled as a Python extension module as well as keeping up the high productivity of Python which is worshipped by the developers.


How to Cythonize your Python code?

✴️ First I'm creating a python file named "hello.pyx"

➡️ A .pyx file is compiled by Cython to a .c file, containing the code of a Python extension module.

# hello.pyx
def say_hello():
    print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

✴️ Now I'm creating a python file named "launch.py" for calling the "hello.pyx" module

➡️ This code will always interpret like normal Python
➡️ It won't be compiled to C

# launch.py
import hello

hello.say_hello()
Enter fullscreen mode Exit fullscreen mode

✴️ Now I'm writing a setuptools named "setup.py" for compiling the "hello.pyx" module

➡️ This is the normal and recommended way from the Cython Documentation.

# setup.py
from setuptools import setup
from Cython.Build import cythonize

setup(
    name='Hello world app', 
    ext_modules=cythonize('*.pyx')
)
Enter fullscreen mode Exit fullscreen mode

✴️ Finally, I'm running this command on the terminal for building

💲 python setup.py build_ext --inplace
Enter fullscreen mode Exit fullscreen mode

🧮 Compilation Workflow of build command

Compilation Workflow

✴️ Let's launch the code for testing

💲 python launch.py
 Hello World!
Enter fullscreen mode Exit fullscreen mode

🌟 You can cythonize your code with just one step using Cythonizer 🌟


Cythonizer is a Python module written by us 🔗. The whole philosophy of writing this module is to make Cythonizing faster by taking less number of steps. That's why the slogan is "Cythonize one step faster" 💘.

➡️ Cythonizer is available on PyPi 🔗
➡️ This means that you can install it via pip

💲 pip install cythonizer
Enter fullscreen mode Exit fullscreen mode

➡️ Now in the terminal, just type

💲 cythonizer YOUR_PY_OR_PYX_FILE
Enter fullscreen mode Exit fullscreen mode

AND DONE 😇, For more details visit Cythonizer's PyPi Page here


Conclusion

So far, I’ve tried to show everything steps by step, and below, the discussion section is open for your opinion to share and of course the questions if any. And don't forget to follow us.

💡 AND SUBSCRIBING to our YouTube TechLearnersInc and Telegram t.me/TechLearners will be amazing.

Top comments (2)

Collapse
 
dynamicsquid profile image
DynamicSquid

Cythonize

That's such a cool word

Collapse
 
techabm profile image
Abdul Basit Minhas

But how to cython decompile?