Measure processing time with Python’s timeit module.

Money and Business

Using the timeit module of the Python standard library, you can easily measure the execution time of a process in your code. This is useful for a quick check.

The following two cases will be discussed here.

  • Measure in a Python file:timeit.timeit(),timeit.repeat()
  • Measurement with Jupyter Notebook:%timeit,%%timeit

Another way is to use time.time() to measure the elapsed time in the program.

Measurements in Python files: timeit.timeit(), timeit.repeat()

As an example, we will measure the processing time of a simple function, test(n), which calculates the sum of n consecutive numbers.

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

If you pass the code you want to measure as a string to the timeit.timeit() function, it will be executed NUMBER of times and the time it took will be returned.
The default value for number is 1,000,000. Note that if you use the default value for a time-consuming process, it will take a lot of time.

By passing globals() as argument globals, the code will be executed in the global namespace.
Without this, the function test and the variable n are not recognized in the example above.

The code to be specified can be a callable object instead of a string, so it can be specified as a lambda expression with no arguments; in this case, the argument globals does not need to be specified.

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

The unit of the result is seconds. Here, the output is the processing time per execution divided by the number of executions.

If you don't divide, the result value will simply become larger as you increase the number of executions.

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

Using the timeit.repeat() function, timeit() can be executed repeatedly. The result will be obtained 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]

Measurement with Jupyter Notebook:%timeit, %%timeit

In Jupyter Notebook (IPython), you can use the following magic commands; there is no need to import the timeit module.

  • %timeit
  • %%timeit

%timeit

In %timeit, specify the target code separated by a space like command line arguments.

By default, the number and repeat in timeit.timeit() are determined automatically. You can also specify them with the -n and -r options.

The results are calculated as mean and standard deviation.

%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 can be used to measure the processing time of an entire cell.

As an example, let's run the same process using NumPy. The -n and -r options can be omitted.

Since we measure the processing time of the entire cell, the following example includes the time 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)

There is no need to specify the target code as an argument for %%timeit. All you have to do is write %%timeit at the beginning of a cell, so it is the easiest to use.

Copied title and URL