Karim Aya

Karim Aya


Speed Up Your Python Code with Cython

Originally published by Lukas Frei at towardsdatascience.com


If you have ever coded in Python, you have probably spent more time waiting for certain code blocks to execute than you would like. While there are ways to make your code more efficient, it will most likely still be slower than C code, for instance. This boils down mainly to the fact that Python is a dynamic programming language and moves many things to runtime that C takes care of during compilation.

Nevertheless, if you, like me, enjoy coding in Python and still want to speed up your code you could consider using Cython. While Cython itself is a separate programming language, it is very easy to incorporate into your e.g. Jupyter Notebook workflow. Upon execution, Cython translates your Python code to C, often times significantly speeding it up.

Installing Cython

In order to be able to use Cython you are going to need a C compiler. Thus, the installation process differs based on your current OS. For Linux, the GNU C Compiler (gncc) is usually present. For Mac OS, you can download Xcode to get the gncc. If you should be using Windows, the installation process is a little more complex. More info here on Cython’s GitHub.

Once you have got your C compiler, all you need to run in your terminal is:

pip install Cython

How to Use Cython

The easiest way to demonstrate Cython’s capabilities is through Jupyter Notebooks. To use Cython in our notebook, we are going to use IPython magic commands. Magic commands start with a percent sign and provide some additional features that are supposed to enhance your workflow. Generally, there are two types of magic commands:

1.    Line magics are denoted by a single ‘%’ and only operates on one line of input

2.  Cell magics are denoted by two ‘%’ and operate on multiple lines of input.

Let’s get started:

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

%load_ext Cython

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


Once you have done that, you are good to go and able to start coding in Cython.

How much faster is Cython?

How much faster Cython is compared to regular Python code really depends on the code itself. For instance, should you be running computationally expensive loops with many variables, Cython will vastly outperform regular Python code. Recursive functions will also tend to make Cython a lot faster than Python.

Let’s demonstrate this with the Fibonacci sequence. This algorithm, to put it simply, finds the next number by adding up the previous two. Here is what that might look like in Python:

def fibonacci(n):
    if n < 0:
        print("1st fibonacci number = 0")
    elif n == 1:
        return 0
    elif n == 2:
        return 1
        return fibonacci(n-1) + fibonacci(n-2)

Let’s make Python work:

As you can see, finding the 39th number in the sequence took 13.3 seconds to compute. Wall time here refers to the total time elapsed from start to finish of the call to the function.

Let’s define the same function in Cython.

What’s going on here? As you can see, we are using some cell magic up top that allows us to use Cython in this cell. I am going to explain what the ‘-a’ option does shortly. Then, we basically take the same code as we did above except for the fact that now we have the ability to make use of static type declarations and define n to be of type integer.

As you can see, by adding ‘-a’ after the magic command, we received annotations that show us how much Python interaction there is in your code. The goal here would be to get rid of all yellow lines and have them have a white background instead. In that case, there would be no Python interaction and all code would run in C. You can also click on the ‘+’ sign next to each line to see the C translation of your Python code.

How much faster is that code? Let’s find out:

In this case, Cython is around 6.75 times faster than Python. This clearly demonstrates the time-saving capabilities of utilizing Cython where it provides the most improvement over regular Python code.

Additional Options

In case you already know C, Cython also allows for the access of C code that the makers’ of Cython have not yet added a ready-to-use declaration for. Using the following code, for instance, you could generate a Python wrapper for a C function and add it to the module dict.

cdef extern from "math.h":
    cpdef double sin(double x)

Cython proves many additional capabilities, such as parallelism, which are all very neatly described in its documentation that you can find here.


If you should, at times, run into the issue of having to wait for too long for your Python code to execute, Cython provides a really neatly integrated and efficient way to speed up your code. On top, it offers many capabilities to further optimize your code should you be a little more familiar with C. I would definitely recommend to thoroughly check out the documentation. If you should have any suggestions or remarks, feel free to reach out to me.

Originally published by Lukas Frei at towardsdatascience.com


Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Data Science, Deep Learning, & Machine Learning with Python

☞ Deep Learning A-Z™: Hands-On Artificial Neural Networks

☞ Machine Learning A-Z™: Hands-On Python & R In Data Science

☞ Python for Data Science and Machine Learning Bootcamp

☞ Machine Learning, Data Science and Deep Learning with Python

☞ [2019] Machine Learning Classification Bootcamp in Python

☞ Introduction to Machine Learning & Deep Learning in Python

☞ Machine Learning Career Guide – Technical Interview

☞ Machine Learning Guide: Learn Machine Learning Algorithms

☞ Machine Learning Basics: Building Regression Model in Python

☞ Machine Learning using Python - A Beginner’s Guide

#python #machine-learning #data-science

What is GEEK

Buddha Community

Speed Up Your Python Code with Cython
Ray  Patel

Ray Patel


top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel


Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Ray  Patel

Ray Patel


50+ Basic Python Code Examples

List, strings, score calculation and more…

1. How to print “Hello World” on Python?

2. How to print “Hello + Username” with the user’s name on Python?

3. How to add 2 numbers entered on Python?

4. How to find the Average of 2 Entered Numbers on Python?

5. How to calculate the Entered Visa and Final Grade Average on Python?

6. How to find the Average of 3 Written Grades entered on Python?

7. How to show the Class Pass Status (PASSED — FAILED) of the Student whose Written Average Has Been Entered on Python?

8. How to find out if the entered number is odd or even on Python?

9. How to find out if the entered number is Positive, Negative, or 0 on Python?

#programming #python #coding #50+ basic python code examples #python programming examples #python code

Ray  Patel

Ray Patel


Common Anti-Patterns in Python

Improve and streamline your code by learning about these common anti-patterns that will save you time and effort. Examples of good and bad practices included.

1. Not Using with to Open Files

When you open a file without the with statement, you need to remember closing the file via calling close() explicitly when finished with processing it. Even while explicitly closing the resource, there are chances of exceptions before the resource is actually released. This can cause inconsistencies, or lead the file to be corrupted. Opening a file via with implements the context manager protocol that releases the resource when execution is outside of the with block.

2. Using list/dict/set Comprehension Unnecessarily

3. Unnecessary Use of Generators

4. Returning More Than One Object Type in a Function Call

5. Not Using get() to Return Default Values From a Dictionary

#code reviews #python programming #debugger #code review tips #python coding #python code #code debugging

August  Larson

August Larson


Live Coding of Python in the Eclipse IDE

This live coding extension makes coders/programmers life easier

Let’s get started…

In this article, I’ll be talking about how to use Live Coding features of Python in Eclipse.

Every time the programmer has to spend a lot of time debugging their code. And still, they failed to debug. This extension will help the coders or programmers to reduce their debugging time. This extension is downloadable in Eclipse IDE.

If you are unaware of how to install the extensions or plugins in eclipse.

Don’t worry at all, I’ll help you out.

Follow these simple steps:-

  • Download Eclipse IDE.
  • After downloading Eclipse, install it on your machine.
  • After installing Eclipse, download Python in Eclipse.
  • Open the Eclipse IDE and set up your workspace.
  • Once done with the above steps, navigate to the **Help **menu tab.
  • In the Help menu tab, click on the “Eclipse Marketplace” option.
  • Search for **“Live Coding in Python” (**or use this link)andclick on the **Install **button.
  • After click on the Install button, accept all the** terms and conditions.**
  • The download and installation process will start.
  • Now, Restart the Eclipse IDE.
  • And start using the Live coding feature.

GIF by Author

Watch my video on Youtube

#python #code #towards-data-science #python-programming #coding #live coding of python in the eclipse ide