search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Measuring execution time of code snippets in Jupyter Notebook

schedule Aug 10, 2023
Last updated
local_offer
Python
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Measuring execution times multiple times

To measure execution time of a single line:

%timeit 2 + 3
15 ns ± 0.85 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

To measure execution time of a single cell:

%%timeit
2 + 3
4 + 5
14.9 ns ± 2.38 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Note that the number of trials performed by %timeit will vary according to how long each trial takes. For instance, consider the following:

%timeit sum(range(100000))
2.34 ms ± 55.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

We see that this code ran only 100 times, while 2+3 ran 100 million times - the longer a code snippet takes for its execution, the smaller the number of trials is.

Measuring execution times once

If for some reason, you just wanted to measure the execution time just once, then use %time instead:

%time 2 + 3
CPU times: user 3 µs, sys: 1 µs, total: 4 µs
Wall time: 16.7 µs

Again, for a block of code, use %%time like so:

%%time
2 + 3
4 + 5
CPU times: user 20 µs, sys: 10 µs, total: 30 µs
Wall time: 43.2 µs

Measuring execution time of each statement

We've seen that the %timeit and %%timeit magic functions are useful when we want to know the execution time of a single statement or a group of statements.

Sometimes we want to know how much time is getting spent on which parts of the program. In technical jargon, we call this profiling, and fortunately Jupyter Notebook provides an extremely easy way to profile our program - the %prun magic function.

Here's an example:

from random import random

def sum_up():
arr = []
for i in range(10000000):
arr.append(random())
arr.sort()
return arr

%prun sum_up()
20000005 function calls in 10.679 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 5.000 5.000 5.000 5.000 {method 'sort' of 'list' objects}
1 3.175 3.175 9.905 9.905 <ipython-input-1239-954f70834392>:3(sum_up)
10000000 0.943 0.000 0.943 0.000 {method 'random' of '_random.Random' objects}
10000000 0.787 0.000 0.787 0.000 {method 'append' of 'list' objects}
1 0.772 0.772 10.677 10.677 <string>:1(<module>)
1 0.002 0.002 10.679 10.679 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

Here, our program has been broken down into 7 individual statements, which are sorted by the total time spent on each statement. We see that the sort() method is the most time-consuming task here. Normally, we would use these results to spot code to optimise.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!