If you’ve ever written code in Python, waiting for certain blocks of code to execute may take longer than you’d like. While there are ways to make your code more efficient, it’s still likely to be slower than C code. This mostly comes down to the fact that Python is a dynamic programming language that moves a lot of things to the runtime that C is responsible for at compile time.

However, if you like writing code in Python as much as I do and still want to speed up your code, you can consider using Cython. While Cython itself is a separate programming language, it’s easy to incorporate into your workflow, such as Jupyter Notebook. When executed, Cython converts your Python code to C, often significantly faster

Install Cython

In order to be able to use Cython, you need a C compiler. Therefore, the installation process will vary depending on your current operating system. For Linux, there is usually a GNUC compiler (GNCC). For Mac OS, you can download Xcode to get GNCC. If you’re supposed to be using Windows, the installation process is a little more complicated. Visit Cython’s GitHub for more information.

Once you have a C compiler, what you need to run on your terminal is:

1pip install Cython
Copy the code

How do I use Cython

The easiest way to demonstrate Cython’s functionality is through The Notebooks of Jupyter. To use Cython in our notebook, we will use the IPython magic command. The Magic command starts with a percent sign and provides additional functionality that enhances the workflow. In general, there are two types of Magic commands:

  1. Line magic is represented by a single ‘%’ and operates on only one line of input
  2. Cell Magic is represented by two “%” s and operates on multi-line input.

Let’s get started:

First, in order to be able to use Cython, we must run:

1%load_ext Cython
Copy the code

Now, whenever we want to run Cython in a code cell, we must first put the following magic command into the cell:

1%%cython
Copy the code

With that done, you can start writing Cython code.

How fast does Cython run?

How much faster Cython is compared to normal Python code really depends on the code itself. For example, if you are running computationally expensive loops with many variables, Cython is vastly superior to regular Python code. Recursive functions also make Cython much faster than Python.

Let’s prove this with the Fibonacci sequence. Simply put, the algorithm finds the next number by adding the first two numbers. Here’s what can happen in Python:



Let’s make Python work:



As you can see, it took 13.3 seconds to find the 39th digit in the sequence. Wall time here refers to the total time taken from the start to the end of a function call.

Let’s define the same function in Cython.



What’s going on here? As you can see, we are using some unit magic that allows us to use Cython in this unit. I’ll explain the “-a” option in a minute. Then, we use basically the same code as above, except now we can use static type declarations and define n as an INTEGER type.

As you can see, by adding ‘-a’ to the magic command, we receive comments that show us how much Python interaction there is in the code. The goal here is to remove all the yellow lines so that they have a white background. In this case, there will be no Python interaction and all the code will run in C. You can also click the “+” symbol next to each line to see the C conversion of Python code.

How much faster is this code? Let’s see:



In this case, Cython is about 6.75 times faster than Python. This clearly demonstrates the time-saving power of taking advantage of Cython, which offers the greatest improvement over regular Python code.

Additional options

If you already know the C language, Cython also gives you access to C code for which the creators of Cython have not yet added ready-made declarations. For example, you can generate Python wrappers for C functions and add them to the module dict using the following code.



Cython demonstrates many additional features, such as parallelism, which are well documented and can be found here.

conclusion

If partners sometimes have problems having to wait too long to execute Python code, Cython provides a very flexible integration and efficient way to speed up code execution. Most importantly, if you’re a little familiar with C, it offers a lot of functionality to further optimize your code. More Python tutorials and tips will be updated from time to time.