Measuring Performance in Python

Measuring Performance in Python

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

def fib(n):
    """
    A bad and inefficient function to compute fibonacci numbers
    """
    if n == 0 or n == 1:
        return 1
    return fib(n-1) + fib(n-2)

print(list(fib(i) for i in range(5)))
[1, 1, 2, 3, 5]

%time just displays the wall time to compute

%time fib(20)
CPU times: user 8.86 ms, sys: 0 ns, total: 8.86 ms
Wall time: 8.84 ms
10946

%timeit will run a function multiple times and displays statistics

%timeit fib(20)
2.18 ms ± 56.1 µ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

import time

start = time.time()
x = fib(20)
end = time.time()
print("{:.2g} sec.".format(end - start))
0.0023 sec.

Memory Use

See this article

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