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))

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/1-16.jpg" data-lazy- height="110" src="data:image/svg xml,” width=”1030″>

%timeit for _ in range(500): True

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/2-16.jpg" data-lazy- height="107" src="data:image/svg xml,” width=”1049″>

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)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/3-14.jpg" data-lazy- height="186" src="data:image/svg xml,” width=”1031″>

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)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/4-13.jpg" data-lazy- height="180" src="data:image/svg xml,” width=”1034″>

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 It executes the code statement times in a loop. If the number is not given, it determines the to get good accuracy.
-r Shows the number of repeats.
-p

Used to calculate the precision of

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

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/49ac9cf0c583f1d7e06205a87f549d76?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

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.