How can I configure my IPython laptop to always show runtime as part of the output?

Sometimes I execute a method that takes a long time to calculate

In [1]:

long_running_invocation()

Out[1]:

Often I’m interested to know how long 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 set up my IPython laptop so that it automatically prints the execution time of each call that I make, as in the following example?

In [1]:

long_running_invocation()

Out[1] (1.59s):
+4
source share
3 answers

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

download it by putting this at the top of your laptop:

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

, .

+6

, , , , , :% time % timeit

ipython

+2

Now you can simply use the magic %%timeat the beginning of the cell as follows:

%%time
data = sc.textFile(sample_location)
doc_count = data.count()
doc_objs = data.map(lambda x: json.loads(x))

which, when executed, will print the output, for example:

CPU times: user 372 ms, sys: 48 ms, total: 420 ms
Wall time: 37.7 s
+1
source

All Articles