Member-only story
Write Your Own C-extension to Speed Up Python by 100x
How to write, compile, package and import your own, superfast C-module into Python
Python is a fantastic language that is very easy to pick up, very fast to develop with, and very clear to read. All these benefits come at a cost: Python is pretty slow compared to some other languages. I can strongly recommend reading this article before continuing to get a clear idea of the problem we’re trying to solve. The goal of this article is to answer this question:
How can we combine Python’s ease of development without sacrificing speed?
The sarcastic answer is to rewrite the project in another language but that’s not what we’re here for. You’re a Python programmer, already have a lot of programs written in Python, and just want to speed up a small part of it. Also: if you are used to writing in Python, the transition to another language like C# or Java might be pretty rough.
We’ll combine the best of two worlds: we extend our program with a small module that we write in C. Python programmers can just import this package, don’t have to know a single line of C, and can still enjoy the 100x speed increase.
Writing in C? Sounds difficult
“Written in C!?” I hear you ask.
“You just talked about a rough transition to Java, now we’re going for C?!”. True, writing code in C might be a little challenging but you’ll see that the 100x speed increase is definitely worth it!
Also, we only have to re-write a small part of our code to C (in our case just a single function).
Isn’t this the same as re-writing the project in another language?
The beauty of the solution that this article describes is that you only have to rewrite the slow parts of your code. Imagine we have programmed an API in Python that receives and analyzes audio files. It makes sense to rewrite the function that analyzes the…