Skip main navigation

Profiling Cython

By default, Cython code does not show up when using cProfile. In this article we discuss how to enable profiling for Cython code.
As the first rule of optimization is to measure performance before starting any optimization work, one should have made profile of the pure Python code before starting any Cythonization. However, in the next optimization cycle one should profile also the Cython code.

By default, Cython code does not show up in the measurements of cProfile. One can, however, enable profiling either for the whole module or for individual functions. In order to enable profiling for the whole function, simply include a global directive at the top of a file:

# cython: profile=True

import numpy as np

def myfunc():

In order to enable profiling for a single function, one can include the
cython decorator before a function definition:

cimport cython

def my_func():

As profiling adds always some overhead, small functions that are called very
frequently can mess up the profile. In these cases one can use the decorator
for disabling profiling for some functions (if profiling is enabled for the
whole module).

# cython: profile=True
cimport cython

def my_func_not_to_profile():

© CC-BY-NC-SA 4.0 by CSC - IT Center for Science
This article is from the free online

Python in High Performance Computing

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now