Python

Timeit in Jupyter Notebook

Jupyter notebook or IPython kernel comes with various magic commands. Complex tasks can easily be completed using these magic commands in very little time and effort and the number of ways available to perform the same job. The preferred consideration factors are speed and code performance to do a similar task. You want to time your code to achieve these factors in most cases. In the Python and Jupyter notebook environment, the “timeit” command similar to the UNIX “time” command provides you some extra help to measure the time execution of your code.

In this guide, we will demonstrate how to use “timeit” in the Jupyter notebook to help you to measure good performance.

Use of timeit in Jupyter Notebook

Fortunately, in Jupyter or IPython notebook, a magic “timeit” command is available to time your code. Timeit magic command in the Jupyter notebook is used to measure the time execution of small code. You don’t need to import the timeit module from a standard library. The “timeit” command starts with the “%” and “%%” symbols that we will discuss in this article.

Most Python users are confused between the use of %timeit and %%timeit commands. Let’s discuss the basic difference between %timeit and %%timeit commands to understand the whole concept about both commands.

The following details will explain to you the difference and use of the timeit command by using % and %% symbols:

%timeit in Jupyter notebook

The “%timeit” is a line magic command in which the code consists of a single line or should be written in the same line for measuring the execution time. In the “%timeit” command, the particular code is specified after the “%timeit” is separated by a space.

This command executes the available code many times and returns the fastest result’s speed. This command will automatically calculate the number of executions needed for the code on a total execution window of 2 seconds.

%timeit Syntax

The following syntax is used to run the “%timeit” command:

%timeit [-n<N>-r<R>[-t|-c] -q -p<P>-o] statement

%timeit max(range(100000))

%timeit for _ in range(500): True

Example

Let’s explain the “%timeit” with the help of the following example:

def test(n):

    return sum(range(n))

n = 10000

%timeit -r 4 -n 10000 test(n)

In the previous source code, the number and repeat are specified, with -n and -r being optional. The repeat and number in “timeit.timeit()” are automatically set by default.

As you can see in the previous output, the standard deviation and mean are calculated of the previous piece of code using %timeit.

%%timeit in Jupyter Notebook

The “%%timeit” command is used to measure the execution time of the entire cell code and can contain several code lines that may be written in the next line. The “%%timeit” is easiest to use because you need to enter “%%timeit” only at the start of the cell. We included the “Numpy” Python library. Therefore, the following example includes the time to import the “Numpy” module:

Example

%%timeit -r 4 -n 10000

import numpy as np

a = np.arange(n)

np.sum(a)

This will calculate the mean and standard deviation of the given code.

Timeit Options

The following options or flags you can specify with the timeit command:

Options Purpose
-n<N> It executes the code statement <N>times in a loop. If the number is not given, it determines the <N>to get good accuracy.
-r<R> Shows the number of repeats.
-p<P> Used to calculate the precision of <P>digits to show the timing result.
-c Use time.clock; default function on Windows to measure the wall time.
-t Use time.time; the default function on Unix measures the wall time.
-q Use for Quiet; do not display any result.
-o Returns the TimeitResult that is further stored in a variable to view more details.

Conclusion

We have seen in this tutorial how to use the timeit in a Jupyter notebook. The %timeit command is used to measure the execution time of a piece of code. We have elaborated the difference between the %timeit and %%timeit command in the Jupyter notebook and how both are used in a program. Different timeit command options are also mentioned in this guide. We hope you found this article helpful. Check out other Linux Hint articles for more tips and information.

About the author

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she's working as a Freelancer & Technical writer. She's a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.