Capture the result of an IPython magic function

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


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).