Cython tutorial: How to speed up Python

How to use Cython and its Python-to-C compiler to give your Python applications a rocket boost

Become An Insider

Sign up now and get FREE access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content. Learn more.

Python is a powerful programming language that is easy to learn and easy to work with, but it is not always the fastest to run—especially when you’re dealing with math or statistics. Third-party libraries like NumPy, which wrap C libraries, can improve the performance of some operations significantly, but sometimes you just need the raw speed and power of C directly in Python.

Cython was developed to make it easier to write C extensions for Python, and to allow existing Python code to be transformed into C. What’s more, Cython allows the optimized code to be shipped with a Python application without external dependencies.

In this tutorial we’ll walk through the steps needed to transform existing Python code into Cython, and to use it in a production application.

A Cython example

Let’s begin with a simple example taken from Cython’s documentation, a not-very-efficient implementation of an integral function:

To continue reading this article register now