Get more than 30X speedup on your Python code
Tags: python, tutorial, productivity, beginners
Published: 2020-12-24 12:16:32

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.

<a href="https://cython.org/"><img alt="Cython Logo" src="https://dev-to-uploads.s3.amazonaws.com/i/gb0raf3y2d1wdf2f5iwi.png" width="50%"/></a>

### **What is Cython?**

[**Cython**](https://cython.org/) is an optimizing static compiler for both the Python programming language and the extended Cython programming language based on well-known [Pyrex](https://en.wikipedia.org/wiki/Pyrex_(programming_language)). 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.

```python
# hello.pyx
def say_hello():
    print("Hello World!")
```

✴️ **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

```python
# launch.py
import hello

hello.say_hello()
```

✴️ **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](https://cython.readthedocs.io/en/latest/src/quickstart/build.html).

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

setup(
    name='Hello world app', 
    ext_modules=cythonize('*.pyx')
)
```

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

```shell
💲 python setup.py build_ext --inplace
```

🧮 **Compilation Workflow of build command**

![Compilation Workflow](https://upload.wikimedia.org/wikipedia/commons/e/e0/Cython_CPython_Ext_Module_Workflow.png)

✴️ **Let's launch the code for testing**

```shell
💲 python launch.py
 Hello World!
```

---
## 🌟 ***You can cythonize your code with just one step using*** [**Cythonizer**](https://github.com/TechLearnersInc/cythonizer) 🌟
---

Cythonizer is a Python module written by us [🔗](https://github.com/TechLearnersInc). 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 [🔗](https://pypi.org/project/cythonizer/)
➡️ This means that you can install it via pip

```shell
💲 pip install cythonizer
``` 

➡️ Now in the terminal, just type

```shell
💲 cythonizer YOUR_PY_OR_PYX_FILE
```
> **AND DONE 😇,** ***For more details visit Cythonizer's PyPi Page [here](https://pypi.org/project/cythonizer/)***

---

## **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**](https://www.youtube.com/c/TechLearnersInc) ***and Telegram*** [**t.me/TechLearners**](https://t.me/TechLearners) ***will be amazing.***