 Picture by Mike, Pexels.

If you’re writing Python code in a Jupyter notebook that is eventually going to be used in production, it’s sensible to consider how long it takes to run. This is especially true if you’re using a cloud service like AWS Lambda, where you’re charged for the amount of time your code runs.

The timeit module is part of the Python standard library, and it’s designed to measure the execution time of small chunks of code. To use timeit in regular Python you need to import the timeit module and write a line of code to call the `timeit()` function.

However, the IPython kernel used in Jupyter notebooks exposes timeit as a magic command, allowing you to time the execution of your notebook code cells using a simple command. In this simple tutorial, I’ll show you how it’s done.

### Create some code to time

To get started, open a Jupyter notebook and create some code so we can measure how long it takes to execute. To give us something that takes a bit of time to run, we’ll write a little function that performs a calculation inside a `for` loop.

``````def test():
for i in range(100000):
i*5
``````

### Measure the code exection time of a function

There are a couple of different ways to use timeit to measure code execution speeds. The standard one is to call `%timeit` on the line before you run the Python function. If you run execute the code below you’ll see that timeit returns the execution time data once the code has completed.

``````%timeit test()
``````
``````2.97 ms ± 26.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
``````

### Measure the code execution time of a cell

The other way to call timeit is by using the `%%timeit` magic command. Rather than calculating the time it takes for a single line of code to execute, `%%timeit` will calculate how long it takes to run all the code inside a Jupyter notebook cell. To use it, you simply place `%%timeit` at the top of a cell, and then run the cell.

``````%%timeit

import pandas as pd

df = pd.DataFrame({'a': range(100000)})
df['b'] = df['a'] * 5

``````
``````657 µs ± 6.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
``````

Matt Clarke, Saturday, January 07, 2023

Matt Clarke Matt is an Ecommerce and Marketing Director who uses data science to help in his work. Matt has a Master's degree in Internet Retailing (plus two other Master's degrees in different fields) and specialises in the technical side of ecommerce and marketing.