IPython%timeit魔术-更改"mean& std"达到“三分之二" [英] IPython %timeit magic - changing output from "mean & std" to "best of 3"

查看:193
本文介绍了IPython%timeit魔术-更改"mean& std"达到“三分之二"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu上将IPython 6.2.1与Eclipse/PyDev集成在一起. Python版本是3.5.2.
我经常看到人们在计时

I am using an IPython 6.2.1 integration with Eclipse/PyDev on Ubuntu. Python version is 3.5.2.
I often see people timing a script like

>>>%timeit for _ in range(1000): True
10000 loops, best of 3: 37.8 µs per loop

当我执行相同的操作时,我的输出是

When I perform the same operation, my output is

>>>%timeit for _ in range(1000): True
20.8 µs ± 353 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Imho,最好的3个"是更好的度量,因此我想更改我的输出. 我同时阅读了 IPython Python timeit 文档.他们俩甚至都没有提到输出可能与"3个最佳"不同.这是Linux/Eclipse/PyDev实现的问题还是有办法更改timeit模块的输出?

Imho, "best of 3" is the better measure, so I would like to change my output. I read both, the IPython and the Python timeit documentation. They both don't even mention, that the output could differ from "best of 3". Is this a question of Linux/Eclipse/PyDev implementation or is there a way to change the output of the timeit module?

P.S .:当我使用timeit时,在Eclipse控制台中也会发生同样的情况,因此IPython在这里可能无关紧要.

P.S.: The same happens in the Eclipse console, when I use timeit, so IPython is probably irrelevant here.

>>>timeit '"-".join(str(n) for n in range(100))'
11 ns ± 0.0667 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

Unutbu指出,您可以从程序内实现所需的行为.运行在此处调用timeit.main() 的第一个脚本确实返回了最好的3个.但是我更喜欢可以运行的版本在Eclipse控制台中进行交互.

Unutbu pointed out that you can achieve the desired behaviour from within a program. Running the first script calling timeit.main() here indeed returns the best of 3. But I would prefer a version that I can run interactively in the Eclipse console.

推荐答案

显示由TimeitResult对象生成(请参见timeit??代码清单).使用-o选项,我可以检查该对象:

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}

看起来,生成best of 3显示的信息仍然存在,但是格式化程序已经消失了.可能是在较旧的版本中找到的.

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)

代码在IPython/core/magics/execution.py

这篇关于IPython%timeit魔术-更改"mean&amp; std"达到“三分之二"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆