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.
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?
# hello.pyx def say_hello(): print("Hello World!")
# launch.py import hello hello.say_hello()
# setup.py from setuptools import setup from Cython.Build import cythonize setup( name='Hello world app', ext_modules=cythonize('*.pyx') )
💲 python setup.py build_ext --inplace
💲 python launch.py Hello World!
You can cythonize your code with just one step using Cythonizer
Cythonizer is a Python module written by us
💲 pip install cythonizer
💲 cythonizer YOUR_PY_OR_PYX_FILE
, For more details visit Cythonizer’s PyPi Page here
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.