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.

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.

From: stackoverflow.com/q/32565829

Back to homepage or read more recommendations: