TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Member-only story

Cython for absolute beginners: 30x faster code in two simple steps

Mike Huls
7 min readMay 22, 2022

--

Let’s speed up our code (image by Abed Ismail on Unsplash)

Python is very easy to work with; clear syntax, the interpreter and duck-typing allow you to develop very quickly. There are some downsides though: if you don’t have to adhere to a strict syntax, then Python has to do some extra work to get your code to run, causing some functions e.g. to execute very slowly because it has to do all those checks again and again.

Combine the ease and speed of developing with Python with the speed of C: the best of both worlds

In this article we’ll take a “slow” function from a vanilla Python project and make it 30x faster. We do this by using a package called Cython that will convert our Python-code to a compiled, superfast piece of C-code that we can directly import into our project again.

A package called CythonBuilder will automatically Cythonize Python code for us in just 2 steps. With CythonBuilder we’ll Cythonize a function from an example project we’ll define below. Let’s code!

But first..

For those unfamiliar with Cython and CythonBuilder we’ll answer some exploratory questions. Then we’ll define…

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Mike Huls
Mike Huls

Written by Mike Huls

I write about interesting programming-related things: techniques, system architecture, software design and how to apply them in the best way. — mikehuls.com

Responses (5)