Can you capture the output of ipython's magic methods? (timeit)

你。 提交于 2019-11-28 22:53:11
Iguananaut

This duplicate question Capture the result of an IPython magic function has an answer demonstrating that this has since been implemented.

Calling the %timeit magic with the -o option like:

%timeit -o <statement>

returns a TimeitResult object which is a simple object with all information about the %timeit run as attributes. For example:

In [1]: result = %timeit -o 1 + 2
Out[1]: 10000000 loops, best of 3: 23.2 ns per loop

In [2]: result.best
Out[2]: 2.3192405700683594e-08

PS: this is in an ipython notebook if that makes a diff.

No it does not.

On dev there is te %%capture cell magic. The other way would be to modify the timeit magic to return value instead of printing, or use the timeit module itself. Patches welcomed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!