Measure Execution Time with timeit in Python

Modified: | Tags: Python, Jupyter Notebook

In Python, you can easily measure the execution time with the timeit module of the standard library.

This article explains how to measure execution time in a Python script and Jupyter Notebook.

You can also use time.time() to measure the elapsed time in your code. See the following article.

Measure execution time in Python script: timeit.timeit(), timeit.repeat()

Let's define a simple function test(n), which calculates the sum of n consecutive numbers, and use it as an example to measure its execution time.

import timeit

def test(n):
    return sum(range(n))

n = 10000
loop = 1000

result = timeit.timeit('test(n)', globals=globals(), number=loop)
print(result / loop)
# 0.0002666301020071842

When you pass the code you wish to measure to timeit.timeit() as a string, it executes the code number times and returns the execution time.

The default value for number is 1,000,000. Be aware that running time-consuming code with the default value can take significant time.

The code is executed in the global namespace by providing globals() as the globals argument. Without this, the function test and the variable n from the example would not be recognized.

timeit.timeit() can also accept a callable object. You can specify lambda expression with no arguments, in which case the globals argument is not necessary.

result = timeit.timeit(lambda: test(n), number=loop)
print(result / loop)
# 0.00027574066299712287

timeit.timeit() returns the time (in seconds) it took to execute the code number times.

In the above example, the time per execution is determined by dividing the total time by the number of executions (number). Without this division, the result simply grows larger as the number of executions increases.

print(timeit.timeit(lambda: test(n), number=1))
print(timeit.timeit(lambda: test(n), number=10))
print(timeit.timeit(lambda: test(n), number=100))
# 0.0003999490290880203
# 0.0038685189792886376
# 0.03517670702422038

You can use timeit.repeat() to repeat the timeit() function. The result is returned as a list.

repeat = 5
print(timeit.repeat(lambda: test(n), repeat=repeat, number=100))
# [0.044914519996382296, 0.039663890027441084, 0.02868645201670006, 0.022745631984435022, 0.023260265996214002]

Measure execution time in Jupyter Notebook: %timeit, %%timeit

In Jupyter Notebook (IPython), you can use the magic commands %timeit and %%timeit to measure the execution time of your code without needing to import the timeit module.

Note that the following sample code cannot be run as a Python script. Refer to the link below for a Jupyter Notebook example (.ipynb).

%timeit

With %timeit, specify the target code after %timeit, separated by a space.

The number and repeat parameters in timeit.timeit() are set automatically by default, but can also be manually specified with the -n and -r options.

The mean and standard deviation of the execution times are calculated.

%timeit test(n)
# 259 µs ± 4.87 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit -r 3 -n 10000 test(n)
# 237 µs ± 6.44 µs per loop (mean ± std. dev. of 3 runs, 10000 loops each)

%%timeit

The magic command %%timeit allows you to measure the execution time of an entire cell.

For example, let's try executing the same process using NumPy. Just like with %timeit, -n and -r are optional.

Note that %%timeit measures the execution time of the entire cell, which means the following example includes the time it takes to import NumPy.

%%timeit -r 3 -n 10000
import numpy as np
a = np.arange(n)
np.sum(a)
# 19.7 µs ± 9.57 µs per loop (mean ± std. dev. of 3 runs, 10000 loops each)

%%timeit provides a convenient way to measure execution time; you simply write %%timeit at the beginning of the cell.

Related Categories

Related Articles