Chris Myers, Andrew Dolgert (original author)
Cornell Center for Advanced Computing

Revisions: 6/2023, 5/2020, 8/2018, 6/2015, 5/2011 (original)

In this topic, we describe a variety of tools and approaches that enable one to incorporate custom compiled code into a Python library or program, either to improve computational performance or to incorporate code written in other languages. This approach is often a useful complement to leveraging the compiled libraries discussed previously.

Broadly speaking, there are two types of approaches, although there are points of intersection between the two:

  • Python-aware compilation frameworks that generate optimized machine code from raw Python source, annotated/decorated Python source, and/or languages that provide extensions of the core Python language to assist compilation
  • Mechanisms and tools for wrapping up compiled code in extension modules that can be called from Python (similar to the way that NumPy, SciPy and others are Python-wrapped versions of compiled libraries)

Which approach or approaches you should use depends a lot on your application. Are you already using NumPy for some of your computations but cannot figure out how to encode everything you need to do in compact array operations? (Maybe you need to unroll your loops and see how a just-in-time compiler might be able to optimize your calculations.) Is the bulk of the processing taking place in a few key functions? (If so, maybe you can use a python-aware compiler to speed up those functions.) Do you have an existing library or program written in another language that you would like to be able to call from Python, e.g., to steer the computation or integrate it alongside other data analysis and visualization tools? (If so, maybe you want to wrap it up in an extension module.)

Objectives

After completing this topic, you should be able to:

  • Discuss approaches to compiling parts of code for use with Python programs
  • Understand tools such as Numba, Cython, CFFI, SWIG, and other to build compiled code and extension modules
Prerequisites

As this topic focuses on accelerating Python programs for scientific computing, it implicitly assumes the reader has some prior experience programming in Python, as well as working knowledge of general programming concepts. The target audience is scientists and engineers who are already programming in Python, and are interested in using Python tools and packages to improve the run time performance of the programs they are developing. If additional introductory material about Python is needed, readers can consult Introduction to Python Programming as well as the documentation on the python.org website.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement