How to measure Cell execution Time in Jupyter Notebook

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. How to measure Cell execution Time in Jupyter Notebook
  2. Measure cell execution time in Jupyter Notebook using %timeit
  3. Measure cell execution time using the time module
  4. Using time.perf_counter() to measure cell execution time

# How to measure Cell execution Time in Jupyter Notebook

Use the %time magic command to measure cell execution time in Jupyter Notebook.

The command measures the execution time of a Python statement or expression.

main.py
%%time for i in range(5000000): i * i

measure cell execution time in jupyter notebook

The %time magic command times the execution of a Python statement or expression.

The command prints the CPU and wall clock times and the value of the expression (if any) is returned.

  • user time - the time that was spent in user-mode code (outside the kernel) within the process.
  • sys time - the time that was spent in the kernel within the process.
  • wall time - wall clock time (time from state to finish of the call).

The time needed for Python to compile the given expression is reported if it takes more than 0.1s.

The %time magic command provides basic timing functionality.

For more fine-grained control, use the %timeit command.

# Measure cell execution time in Jupyter Notebook using %timeit

The %timeit magic command prints the time execution of a Python statement or expression.

main.py
%%timeit for i in range(5000000): i * i

print time execution using timeit

The %timeit command runs the statement in the cell multiple times and returns the average runtime for some statements and the standard deviation.

# Measure cell execution time using the time module

You can also use the time module to measure cell execution time in Jupyter Notebook.

main.py
import time start_time = time.time() for i in range(5000000): i * i print(time.time() - start_time)

measure cell time using time module

The time.time() method returns the time in seconds since the epoch as a floating point number.

We call the method before the code starts executing and store the result in a variable.

The last step is to call the method again after the code block has finished executing and subtract the start time from the end time.

As shown in the screenshot, the code in the example is executed in 388ms.

You might have to use the round() function if you want to round the result to N digits after the decimal.

main.py
import time start_time = time.time() for i in range(5000000): i * i print(round(time.time() - start_time, 2))

The round function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe number of digits after the decimal, the number should have after the operation (optional)

The code sample rounds the result to 2 decimal places.

# Using time.perf_counter() to measure cell execution time

You can also use the time.perf_counter() method to measure cell execution time.

main.py
import time start_time = time.perf_counter() for i in range(5000000): i * i print(time.perf_counter() - start_time)

measure cell execution time using perf counter

The time.perf_counter() method returns the value (in fractional seconds) of a performance counter.

The method includes time elapsed during sleep and is system-wide.

There is also a time.process_time() method that does not include time elapsed during sleep.

main.py
import time start_time = time.process_time() for i in range(5000000): i * i print(time.process_time() - start_time)

measure cell execution time using process time

You can also use the time.monotonic() method in a similar way.

main.py
import time start_time = time.monotonic() for i in range(5000000): i * i print(time.monotonic() - start_time)

calculate execution time using time monotonic

The time.monotonic() method returns the value of a monotonic clock in fractional seconds.

The clock is not affected by the system clock updates.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.