IPython %timeit magic - changing output from “mean & std” to “best of 3”

送分小仙女□ 提交于 2019-12-04 14:18:15

The display is generated by a TimeitResult object (see the timeit?? code listing). With the -o option I get that object which I can examine:

In [95]: %timeit -o np.einsum('bdc,ac->ab', a, b, optimize=False)
170 µs ± 27.5 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Out[95]: <TimeitResult : 170 µs ± 27.5 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)>
In [96]: res = _
In [97]: print(res)
170 µs ± 27.5 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [98]: vars(res)
Out[98]: 
{'_precision': 3,
 'all_runs': [1.6981208299985155,
  1.6976570829865523,
  1.6978941220149864,
  1.6976559630129486,
  1.6976608499826398,
  1.697147795028286,
  1.6977746890042908],
 'best': 0.0001697147795028286,
 'compile_time': 0.0,
 'loops': 10000,
 'repeat': 7,
 'timings': [0.00016981208299985155,
  0.00016976570829865524,
  0.00016978941220149863,
  0.00016976559630129485,
  0.00016976608499826398,
  0.0001697147795028286,
  0.0001697774689004291],
 'worst': 0.00016981208299985155}

It looks like the information to generate the best of 3 display is still there, but the formatter is gone. It might be found in an older version.

@property
def average(self):
    return math.fsum(self.timings) / len(self.timings)

Code is in IPython/core/magics/execution.py

This syntax std/mean/dev was added to IPython in 5 Oct 2016. See the issue #9984 https://github.com/ipython/ipython/pull/9984 about this improvement. And the implementation is here:

"Added mean + stdv to the %timeit magic - New tests pending - "

https://github.com/ipython/ipython/commit/509d8539c555ede6222d13cf1693388429213853#diff-ee52fbe4422737ccaa3d6d0b15ea5189

but syntax "best of 3" comes from python timeit module in main() function and this is generally the same in python2 and 3.

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