Member-only story
Getting started with Cython: How to perform >1.7 billion calculations per second in Python
Combine the ease of Python with the speed of C
The main advantage of Python is that it is very developer-friendly and easy to pick up. These design choices have a major downside, though; they cause Python to execute significantly slower than some other languages. This article will show you how to eat your cake and have it too. We’re going to take the bottleneck out of our Python code and use Cython to speed it up extremely. I can strongly recommend reading this article before continuing to get a clear idea of the problem we’re trying to solve. Cython will help us to:
- use Python-like syntax to write code that Cython then will generate C-code with. We won’t have to write in C ourselves
- compile the C-code and package it into a Python module that we can import (just like
import time
) - improve execution speeds of our program >70x
- brag to colleagues about our superfast code
I’ve split this article into 4 parts: First, we install dependencies and setup in part A. Then, in part B we just focus on getting Cython code to run Python. Once this is done we optimize our Cython code in part C using a handy built-it tool that’ll tell you exactly where…