Capture the result of an IPython magic function

后端 未结 3 472
时光取名叫无心
时光取名叫无心 2021-01-17 11:54

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

So the following code...

import tim         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-17 12:38

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

提交回复
热议问题