Discover more from Daily Dose of Data Science
The Most Under-appreciated Technique To Speed-up Python
...within minimal effort.
Python’s default interpreter — CPython, isn’t smart.
It serves as a standard interpreter for Python and offers no built-in optimization.
Instead, use the Cython module.
CPython and Cython are different. Don’t get confused between the two.
Cython converts your Python code into C, which is fast and efficient.
Steps to use the Cython module:
Load the Cython module (in a separate cell of the notebook):
Add the Cython magic command at the top of the cell:
When using functions, specify their parameter data type.
def func(int number): ...
Define every variable using the “
cdef” keyword and specify its data type.
cdef int a = 10
Once done, Cython will convert your Python code to C, as depicted below:
This will run at native machine code speed. Just invoke the method:
The speedup is evident from the image below:
Python code is slow.
But Cython provides a 100x speedup.
Why does this work?
Essentially, Python is dynamic in nature.
For instance, you can define a variable of a specific type. But later, you can change it to some other type.
a = 10 a = "hello" # Perfectly legal in Python
These dynamic manipulations come at the cost of run time. They also introduce memory overheads.
However, Cython lets you restrict Python’s dynamicity.
We avoid the above overheads by explicitly specifying the variable data type.
cdef int a = 10 a = "hello" ## Raises error
The above declaration restricts the variable to a specific data type. This means the program would never have to worry about dynamic allocations.
This speeds up run-time and reduces memory overheads.
Isn’t that cool?
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (350+ pages) with 250+ tips.
👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.
👉 Over to you: What are some other ways to speed up Python?
Thanks for reading!
Whenever you’re ready, here are a couple of more ways I can help you:
Get the full experience of the Daily Dose of Data Science. Every week, receive two curiosity-driven deep dives that:
Make you fundamentally strong at data science and statistics.
Help you approach data science problems with intuition.
Teach you concepts that are highly overlooked or misinterpreted.
Promote to over 28,000 subscribers by sponsoring this newsletter.
👉 Tell the world what makes this newsletter special for you by leaving a review here :)
👉 If you love reading this newsletter, feel free to share it with friends!