Simple way to measure cell execution time in ipython notebook
Asked Answered
A

14

345

I would like to get the time spent on the cell execution in addition to the original output from cell.

To this end, I tried %%timeit -r1 -n1 but it doesn't expose the variable defined within cell.

%%time works for cell which only contains 1 statement.

In[1]: %%time
       1
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 5.96 µs
Out[1]: 1

In[2]: %%time
       # Notice there is no out result in this case.
       x = 1
       x
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs

What's the best way to do it?

Update

I have been using Execute Time in Nbextension for quite some time now. It is great.

Update 2021-03

As of now, this is the correct answer. Essentially, %%time and %%timeit both now work as one would expect.

Antibiotic answered 14/9, 2015 at 13:15 Comment(2)
do you really need to time the display of the value? why not just put the x display line in the next cell?Gerbil
It is worth noticing that you can also use %time if you just want to time a line: %time my_one_line_code (could be a call to a function).Slowly
C
86

That was only a problem in old versions.

All you need to do now is put %%time at the top of the cell.

enter image description here

%%time measures how long it took something to run. It’s better for reporting on long-running operations than for doing low-level optimization.

%%timeit is a benchmarking tool that runs statements over and over to give the average runtime for some statements, as well as the standard deviation. Because of the way in which the statements are repeatedly executed, the variables created in %%timeit cells are not available in other cells.

enter image description here

%%timeit uses the python timeit module. The docs for that say,

It avoids a number of common traps for measuring execution times. See also Tim Peters’ introduction to the “Algorithms” chapter in the Python Cookbook, published by O’Reilly.

I hope that that module is still relevant, as the reference it refers to describes issues such as (1) workarounds for Windows 98 only updating time.time() 18.2 times per second, and (2) jamming all the statements onto one line to avoid the bytecode overhead of incrementing the line number counter.


The currently top-rated answer, as well as some of the other outdated ones—which should be deleted because they are now highly misleadingdo have useful comments indicating that those answers are not correct:

Chapman answered 16/3, 2021 at 21:57 Comment(0)
C
603

The only way I found to overcome this problem is by executing the last statement with print.

Do not forget that cell magic starts with %% and line magic starts with %.

%%time
clf = tree.DecisionTreeRegressor().fit(X_train, y_train)
res = clf.predict(X_test)
print(res)

Notice that any changes performed inside the cell are taken into consideration in the next cells:[example]

Connel answered 12/7, 2016 at 3:5 Comment(7)
Now %%time works even when the last statement is not print, as @rhaps0dy pointed out above.Rooker
display(res) also works and is the preferred solution when trying to display a pandas dataframe or something else that requires a stylized output.Aida
@Aida Yes that is correct and makes it easy portable for databricks/spark notebooks too.Agape
Isn't it a problem when we implement the 1st cell %%time and a=1 but the 2nd cell doesn't know what a is?Godhood
@Jason, yes, this is why I mentioned it and used words like 'notice' and 'counter intuitive'.Connel
@SalvadorDali can you take a look at this question, stackoverflow.com/q/58926940/5904928 why there is huge difference between lstm final output vs state output.Pontiff
FYI. I found that variables in the tested cell are now taken into consideration into the next cells. (20/02/2020) - FeiBonhomie
U
122

An easier way is to use ExecuteTime plugin in jupyter_contrib_nbextensions package.

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable execute_time/ExecuteTime
Uribe answered 17/5, 2018 at 6:10 Comment(1)
And if pip doesn't work, conda or direct install options are mentioned on the github github.com/ipython-contrib/jupyter_contrib_nbextensionsVincentia
D
89

%time and %timeit now come part of ipython's built-in magic commands

Dunfermline answered 14/9, 2015 at 13:59 Comment(0)
C
86

That was only a problem in old versions.

All you need to do now is put %%time at the top of the cell.

enter image description here

%%time measures how long it took something to run. It’s better for reporting on long-running operations than for doing low-level optimization.

%%timeit is a benchmarking tool that runs statements over and over to give the average runtime for some statements, as well as the standard deviation. Because of the way in which the statements are repeatedly executed, the variables created in %%timeit cells are not available in other cells.

enter image description here

%%timeit uses the python timeit module. The docs for that say,

It avoids a number of common traps for measuring execution times. See also Tim Peters’ introduction to the “Algorithms” chapter in the Python Cookbook, published by O’Reilly.

I hope that that module is still relevant, as the reference it refers to describes issues such as (1) workarounds for Windows 98 only updating time.time() 18.2 times per second, and (2) jamming all the statements onto one line to avoid the bytecode overhead of incrementing the line number counter.


The currently top-rated answer, as well as some of the other outdated ones—which should be deleted because they are now highly misleadingdo have useful comments indicating that those answers are not correct:

Chapman answered 16/3, 2021 at 21:57 Comment(0)
N
73

Use cell magic and this project on github by Phillip Cloud:

Load it by putting this at the top of your notebook or put it in your config file if you always want to load it by default:

%install_ext https://raw.github.com/cpcloud/ipython-autotime/master/autotime.py
%load_ext autotime

If loaded, every output of subsequent cell execution will include the time in min and sec it took to execute it.

Notation answered 18/4, 2016 at 9:27 Comment(4)
this no longer works, since %install_ext is deprecated. Is there an alternative?Naldo
There is a Pull Request adressing this issue(github.com/cpcloud/ipython-autotime/pull/5) then you can try pip install ipython-autotimeBrenda
Now %%time works even when the last statement is not print.Kimberli
to summarize: 1) pip install ipython-autotime 2) type into the first cell in jupyter : %load_ext autotimeEffloresce
L
52
import time
start = time.time()
"the code you want to test stays here"
end = time.time()
print(end - start)
Livingstone answered 15/7, 2019 at 12:12 Comment(2)
What are the units of this time, seconds?Pattison
time.time() returns times in seconds.Nomi
R
24

You can use timeit magic function for that.

%timeit CODE_LINE

Or on the cell

%%timeit 

SOME_CELL_CODE

Check more IPython magic functions at https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb

Reprehensible answered 28/2, 2019 at 4:52 Comment(0)
H
19

I simply added %%time at the beginning of the cell and got the time. You may use the same on Jupyter Spark cluster/ Virtual environment using the same. Just add %%time at the top of the cell and you will get the output. On spark cluster using Jupyter, I added to the top of the cell and I got output like below:-

[1]  %%time
     import pandas as pd
     from pyspark.ml import Pipeline
     from pyspark.ml.classification import LogisticRegression
     import numpy as np
     .... code ....

Output :-

CPU times: user 59.8 s, sys: 4.97 s, total: 1min 4s
Wall time: 1min 18s
Helvetia answered 16/11, 2017 at 16:18 Comment(2)
Does this execute the cell code a default no. of times and then takes the average? And what about the first statement as the 'setup code'?Gilmour
It executes the code onceHispanic
A
19

The Simplest way to measure cell execution time in ipython notebook is by using ipython-autotime package.

Install the package in the begining of the notebook

pip install ipython-autotime

and then load the extension by running below

%load_ext autotime

Once you have loaded it, any cell run after this ,will give you the execution time of the cell.

And dont worry if you want to turn it off, just unload the extension by running below

%unload_ext autotime

It is pretty simple and easy to use it whenever you want.

And if you want to check out more, can refer to ipython-autime documentation or its github source

Abut answered 21/7, 2021 at 20:8 Comment(0)
A
16

This is not exactly beautiful but without extra software

class timeit():
    from datetime import datetime
    def __enter__(self):
        self.tic = self.datetime.now()
    def __exit__(self, *args, **kwargs):
        print('runtime: {}'.format(self.datetime.now() - self.tic))

Then you can run it like:

with timeit():
    # your code, e.g., 
    print(sum(range(int(1e7))))

% 49999995000000
% runtime: 0:00:00.338492
Aurelea answered 27/2, 2017 at 17:18 Comment(0)
C
16

If you want to print wall cell execution time here is a trick, use

%%time
<--code goes here-->

but here make sure that, the %%time is a magic function, so put it at first line in your code.

if you put it after some line of your code it's going to give you usage error and not gonna work.

Cauley answered 25/6, 2020 at 7:29 Comment(0)
C
7

Sometimes the formatting is different in a cell when using print(res), but jupyter/ipython comes with a display. See an example of the formatting difference using pandas below.

%%time
import pandas as pd 
from IPython.display import display

df = pd.DataFrame({"col0":{"a":0,"b":0}
              ,"col1":{"a":1,"b":1}
              ,"col2":{"a":2,"b":2}
             })

#compare the following
print(df)
display(df)

The display statement can preserve the formatting. screenshot

Circuitous answered 15/6, 2017 at 22:23 Comment(1)
Does this execute the cell code a default no. of times and then takes the average? And what about the first statement as the 'setup code'?Gilmour
K
5

you may also want to look in to python's profiling magic command %prunwhich gives something like -

def sum_of_lists(N):
    total = 0
    for i in range(5):
        L = [j ^ (j >> i) for j in range(N)]
        total += sum(L)
    return total

then

%prun sum_of_lists(1000000)

will return

14 function calls in 0.714 seconds  

Ordered by: internal time      

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    5    0.599    0.120    0.599    0.120 <ipython-input-19>:4(<listcomp>)
    5    0.064    0.013    0.064    0.013 {built-in method sum}
    1    0.036    0.036    0.699    0.699 <ipython-input-19>:1(sum_of_lists)
    1    0.014    0.014    0.714    0.714 <string>:1(<module>)
    1    0.000    0.000    0.714    0.714 {built-in method exec}

I find it useful when working with large chunks of code.

Kreda answered 10/8, 2018 at 11:26 Comment(0)
D
5

When in trouble what means what:

?%timeit or ??timeit

To get the details:

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

Time execution of a Python statement or expression using the timeit
module.  This function can be used both as a line and cell magic:

- In line mode you can time a single-line statement (though multiple
  ones can be chained with using semicolons).

- In cell mode, the statement in the first line is used as setup code
  (executed but not timed) and the body of the cell is timed.  The cell
  body has access to any variables created in the setup code.
Delphadelphi answered 12/8, 2019 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.