Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Measuring Performance in Python

Open in Colab

In this section, we’ll cover how to measure performance using tools from Python and IPython

IPython

If you are using IPython (such as in a Jupyter notebook), there are two “magics” that are very useful: %time and %timeit

[1, 1, 2, 3, 5]

%time just displays the wall time to compute

CPU times: user 2.99 ms, sys: 986 µs, total: 3.98 ms
Wall time: 3.69 ms
10946

%timeit will run a function multiple times and displays statistics

2.37 ms ± 216 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

This takes longer to compute, but will be more accuratate

Python

If you’re not using IPython, or want more control over your timing operations, you’ll need to write your own timing code.

The simplest way to do this is to use the time module

0.0031 sec.

Memory Use

See this article

Tracking memory use is not as simple as it can be in some languages because Python uses