Timeit#

By running the line to measure a lot of time (see below), timeit give a median value of consumed time. Simply call it this way (in a Jupyter cell):

import timeit

%timeit to_measure()

The output looks like this:

1.09 µs ± 32.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Timeit typically run the method for a 1’000’000 times so that the result is a median useful value. If the method takes time, timeit will run it less times (down to 10’000 or lower).

This is a parameter that you can force if you feel the need ;)

There is also the availability to give a defined timer.

Command line#

It is also possible to call it from command line (both of those examples are from the documentation):

$ python3 -m timeit '"-".join(str(n) for n in range(100))'

Or from Python interpreter:

>>> import timeit
>>> timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)

profiling benchmark snakeviz timeit Python