Cython: C Extensions for Python

A few years back I was learning something that is CPU intensive. It was written in PHP at first, wasn’t satisfied with the performance. Rewrote the whole thing in Python, it was worse, same thing with PyPy only a bit better. So f*ck it I said to myself, let’s learn Cython.

Problem Domain

Let me reiterate what I want to achieve:

  1. Write codes in Python (sort of)
  2. Do CPU intensive tasks
  3. Fast

Also, I don’t want to waste too much time researching and not getting anything done. So there you go, the perfect reasonings to learn something new: Cython.

For demo purposes, let’s write some codes and Cythonize.

I’m using a 2015 i5 Macbook Air and here is my result using plain Python:

Our first exercise will just Cythonize the plain source code above, this is what we do.

As you can see there’s a new file called cpuload.c generated by Cython. Satisfy your curiosity, go ahead and see what’s inside.

Next up, we need to compile the C source code into a C extension we can use in our Python code. Now we need to create a file to do this.

Ok let’s compile then.

Next up is to benchmark the C extension, let’s write some codes.

If you see the code above, it looks the same with the Cythonized codes right? The only difference is that instead of defining the load() function, we import it from from the C Extension cpuload. So how did we fare so far in performance?

The first plain Python codes gave us 13,018,765.56 operations/second while the Cythonized codes gave us 25,903,031.00 operations/second. This is a 198% speed up from plain Python. Happy right?

This concludes the first Cython piece, I will be writing more about how to gain more speedups and also some pitfalls. Until then! Cheers!

Like what you read? Give Batista Harahap a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.