Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure runtime of a Jupyter Notebook code cell

It seems that in Spyder (IPython3 Kernel) one can easily time a code cell by running the %%time or %%timeit command at the top of the code cell:

#%%
%%time # or %%timeit which measures average runtime from multiple runs
....

#%% (the previous cell ends and the next begins)

Running the above code can get the runtime of the cell defined by the pair of #%%. This is how things work in Spyder, but doesn't quite work in the Jupyter Notebook environment.

In Jupyter code cells aren't defined by #%% delimiters, rather they are created by clicking a button in the menu bar. And as far as I tried, the command %%time and %%timeit both raise compilation error. It seems that Jupyter can't recognise them, but it's strange because my Jupyter actually uses the same IPython kernel as Spyder does. One thing that works in Jupyter is the %time and %timeit commands, but they can only measure the runtime of a one-line code, i.e., must be formulated like

%time blah blah

and it turns out I can't even measure a for loop which consists of more than one line. So this method is not desirable for me. Is there just any way to evaluate a cell runtime using the magic command %time(it) and the like in Jupyter?

(PS: If as in Spyder I attach a %time command at the top of a cell it will give Wall time: 0 ns because there is nothing following it in that same line and it actually measures nothing.)

like image 571
Vim Avatar asked Apr 09 '17 14:04

Vim


2 Answers

Please put %%time at the very start of the cell even before any comments. This worked for me.

like image 159
Rizwan Hamid Randhawa Avatar answered Oct 14 '22 22:10

Rizwan Hamid Randhawa


It depends on how you want to use the time information...

If you simply want to know how long a cell took to execute for your own knowledge, then the ExecuteTime notebook extension (https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/7672d429957aaefe9f2e71b15e3b78ebb9ba96d1/src/jupyter_contrib_nbextensions/nbextensions/execute_time) is a nice solution as it provides time information for all code cells automatically, meaning reduced code maintenance as you don't have to add timing code all over the place. It also writes the last executed date stamp which is useful if you're using the notebook as a scientific log-book.

However, if you want to use the time information programatically, you will need to add code to capture the time information into a variable. As per this answer (Get time of execution of a block of code in Python 2.7), you can use the timeit module:

import timeit
start_time = timeit.default_timer()
# code you want to evaluate
elapsed = timeit.default_timer() - start_time

Obviously, this is not as neat as using cell magic but should get the job done.

As for how / if you can achieve the latter using cell magic, I don't know.

like image 30
Biggsy Avatar answered Oct 14 '22 22:10

Biggsy