17

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.)

Vim
  • 1,436
  • 2
  • 19
  • 32
  • 8
    `%%time` and `%%timeit` are valid IPython cell magics, eithet should work as the first line in a notebook cell. What version of Jupyter and IPython are you using? – cco Apr 10 '17 at 03:31
  • @cco Jupyter 4.3.1 and IPython 4.2.1, should be up to date. – Vim Apr 10 '17 at 04:00
  • Please show the cell and the error you get when you try to use `%%time` - if `%time` works, so should `%%time`. – cco Apr 10 '17 at 04:06
  • 1
    @cco I know where things went wrong. I thought comments didn't count so I attached the command below a header command. – Vim Apr 10 '17 at 04:13

6 Answers6

39

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

Mohamed Taher Alrefaie
  • 15,698
  • 9
  • 48
  • 66
18

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.

Biggsy
  • 1,306
  • 1
  • 12
  • 28
  • 3
    Thanks for the suggestion. Yes I can achieve the latter using %%timeit command at the very beginning of each cell. – Vim May 29 '17 at 10:35
4

To avoid use of %% again in each cell

Automatic cell execution time

open cmd Run command one by one

  1. pip install jupyter_contrib_nbextensions
  2. jupyter contrib nbextension install --user
  3. jupyter nbextension enable spellchecker/main
  4. jupyter nbextension enable codefolding/main
Nicholas Huh
  • 85
  • 1
  • 2
  • 11
Welcome_back
  • 1,245
  • 12
  • 18
4

For basic timing functionality use the %%time cell magic at the beginning of the cell.

%%timeit allows for some more meaningful timing experiments by repeating the measurements. You can use the default options or define your own, for instance here's three runs with 100 loops each:

%%timeit -n100 -r3
import math; n = 2       # the first line is the setup and doesn't get timed 
math.sqrt(n)

# Out:
# 255 ns ± 20.7 ns per loop (mean ± std. dev. of 3 runs, 100 loops each)
  • -n<N>: execute the given statement times in a loop. If is not provided, is determined so as to get sufficient accuracy.

  • -r<R>: number of repeats , each consisting of loops, and take the best result. Default: 7

Both %%time and %%timeit can also be used as line magics (with one %) for timing a single line of code.

user2314737
  • 27,088
  • 20
  • 102
  • 114
1

Like Rizwan's answer, I found this to be the minimal way:

Put %%time at the very start of the cell even before any comments.

2nd option is to use: timeit library, which I found to be text heavy (personal opinion).

enter image description here

sumon c
  • 739
  • 2
  • 10
  • 18
0

In addition to Jai Mahesh answer: After the 4 commands, in my Linux Mint system, I needed to enable the execution time manually to see the measured execution time. For this I installed the configurator: https://github.com/Jupyter-contrib/jupyter_nbextensions_configurator

Then run Jupyter Notebook and go with your browser into the configurator (usually this is going to http://localhost:8888/nbextensions) and enable manually 'Execute time'. View in the extension configurator with enabled extension 'Execute Time'

i.i.k.
  • 135
  • 5