CFFI, Ctypes, Cython, Cppyy: how to run C code from Python
09-03, 14:00–15:30 (UTC), Track 3 (Oteiza)

Python is flexible, C and C++ are fast. How to use them together? There are many ways to call C code from Python, we will learn about the major ones, find out when you would prefer to use one over the other.


Using the Jupyter notebook and a compiler, we will start with a pure python implementation of a mandlebrot image. Then we will write the computationally heavy part of the code in C, and learn how to call it from Ctypes (part of the Python standard library), CFFI (a newer and better Ctypes alternative), Cython (a compiler from Python to C), and CPPYY (like Ctypes and CFFI, but for C++).

Along the way we will stop to reflect on the advantages and disadvantages of each technique in terms of speed of development, runtime overhead, maintainability, and readability.

The participants will come away with an understanding of the tools, their strengths and weaknesses, and how to use them.

Please be sure you have a computer with anaconda python installed and a compiler (for windows users - Visual Studio 2019 is recommended. Others should have a functioniong gcc or clang). You should also download the git repo and be sure you can run the first few cells that involve compilation (before the ctypes discussion). Also please be sure to preinstall cppyy.


Project Homepage / Git

https://github.com/mattip/c_from_python

Project Homepage / Git Abstract as a tweet

Ctypes, CFFI, Cython, CPPYY - how to call C/C++ from Python

Python Skill Level

professional

Domain Expertise

some

Domains

Big Data, General-purpose Python, Image Processing, Scientific data flow and persistence, Vector and array manipulation

Matti is a core developer of PyPy, contributing to the internal numpy implementation _micronumpy and to the layer that allows python c-extension modules to run on the PyPy python interpreter. He has been active in the open source community both as a contributor, teacher, and presenter at conferences. Since April 2018, he works full-time developing NumPy, employed by BIDS

This speaker also appears in: