15

I'm trying to capture the resulting object of IPython Notebook magic function. Specifically %timeit

So the following code...

import time
def say_hello(n):
    time.sleep(n)
    print "hello"

t = %timeit say_hello(5)

Prints to stdout:

1 loops, best of 3: 5 s per loop

However, I'd like to capture the result of %timeit say_hello(5) in the variable t.

A resulting object called TimeitResult is generated by %timeit, but I can't figure out how to access it from within a Notebook.

I'd like a cleaner solution than having to manually capture stdout using sys.stdout tricks (this code will be part of a presentation so I'm trying to keep it as straight forward as possible). Anyone have any ideas?

eric chiang
  • 2,575
  • 2
  • 20
  • 23
  • Do you want the string of the output, or the TimeitResult object? – dsemi Aug 13 '14 at 15:07
  • 1
    possible duplicate of [Can you capture the output of ipython's magic methods? (timeit)](http://stackoverflow.com/questions/17310752/can-you-capture-the-output-of-ipythons-magic-methods-timeit) – Framester Nov 26 '14 at 16:55

3 Answers3

22

In the source file you linked to, the docstring shows the options for running the timeit magic function; one of which is returning an object result:

-o: return a TimeitResult that can be stored in a variable to inspect
        the result in more details.

So, if you run

obj = %timeit -o somefunc()

obj will reference the result object that was returned (hint: use tab completion on the object, that will show you the attributes it has).

dsemi
  • 640
  • 5
  • 10
  • Awesome, was exactly what I was looking for! – eric chiang Aug 13 '14 at 16:09
  • Is there any documentation of the `TimeitResult` object so that I don't have to use the tab completion trick and figure out all attributes myself? – stefanbschneider Dec 04 '19 at 15:59
  • 1
    Couldn't find any official docs, but the docstring in the [source](https://github.com/ipython/ipython/blob/61a4f1f7c11c30372fed53d6c3a90db2f692109f/IPython/core/magics/execution.py#L66) shows all of the attributes. – dsemi Dec 06 '19 at 00:21
1

An example of consuming the TimeItResult output:

myarray = (3,2,1)
sorttime = %timeit -n1 -r3 -o myarray.sort() 
print(sorttime.best)

Look here for the other TimeItResult attributes: https://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.execution.html

Larry C
  • 23
  • 7
1

Complementing @dsemi's answer: Use -o to save the timeit result into a variable, e.g.:

obj = %timeit -o somefunc()

The docstring documentation of the TimeitResult when using tab completion shows the available attributes:

Object returned by the timeit magic with info about the run.

Contains the following attributes :

loops: (int) number of loops done per measurement
repeat: (int) number of times the measurement has been repeated
best: (float) best execution time / number
all_runs: (list of float) execution time of each run (in s)
compile_time: (float) time of statement compilation (s)
stefanbschneider
  • 5,460
  • 8
  • 50
  • 88