Simple way to measure cell execution time in ipython notebook

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.

14 Answers
14

Leave a Comment