Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure my IPython notebook so it always shows the execution time as part of the output?

Sometimes I execute a method that takes long to compute

In [1]:

long_running_invocation()

Out[1]:

Often I am interested in knowing how much time it took, so I have to write this:

In[2]:
    import time

    start = time.time()
    long_running_invocation()
    end = time.time()
    print end - start
Out[2]: 1024

Is there a way to configure my IPython notebook so that it automatically prints the execution time of every call I am making like in the following example?

In [1]:

long_running_invocation()

Out[1] (1.59s):
like image 732
Kuba Avatar asked Dec 15 '22 18:12

Kuba


2 Answers

This ipython extension does what you want: https://github.com/cpcloud/ipython-autotime

load it by putting this at the top of your notebook:

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

Once loaded, every subsequent cell execution will include the time it took to execute as part of its output.

like image 65
adamhajari Avatar answered Apr 05 '23 22:04

adamhajari


i haven't found a way to have every cell output the time it takes to execute the code, but instead of what you have, you can use cell magics: %time or %timeit

ipython cell magics

like image 40
jgaw Avatar answered Apr 05 '23 22:04

jgaw