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 898 µs, sys: 68 µs, total: 966 µs
Wall time: 969 µs
10946

%timeit will run a function multiple times and displays statistics

%timeit fib(20)
961 µs ± 8 µs per loop (mean ± std. dev. of 7 runs, 1,000 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.001 sec.

Memory Use#

See this article

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

TODO:#

tracemalloc package