【问题标题】:IPython %timeit magic - changing output from "mean & std" to "best of 3"IPython %timeit 魔术 - 将输出从“mean & std”更改为“best of 3”
【发布时间】:2018-07-21 20:38:07
【问题描述】:

我在 Ubuntu 上使用 IPython 6.2.1 与 Eclipse/PyDev 集成。 Python 版本为 3.5.2。
我经常看到人们对像

这样的脚本进行计时
>>>%timeit for _ in range(1000): True
10000 loops, best of 3: 37.8 µs per loop

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

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

恕我直言,“最好的 3”是更好的衡量标准,所以我想改变我的输出。 我阅读了IPythonPython timeit 文档。他们甚至都没有提到输出可能与“最佳 3”不同。这是 Linux/Eclipse/PyDev 实现的问题,还是有办法更改 timeit 模块的输出?

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

>>>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 指出,您可以在程序中实现所需的行为。运行 first script calling timeit.main() here 确实返回了 3 个中最好的。但我更喜欢可以在 Eclipse 控制台中交互式运行的版本。

【问题讨论】:

  • best of 3 是一种较旧的显示样式。它可能在某些ipython 版本中发生了变化。 timeit 模块可能会发生变化,但我仍在使用 Py 3.5。
  • %timeit??看来,显示是由IPython.core.magics.execution.TimeitResult(loops, repeat, best, all_runs, compile_time, precision)对象生成的。
  • 我不使用 Eclipse,但我猜它使用的是ipython,而不是直接使用timeit

标签: python eclipse python-3.x ipython timeit


【解决方案1】:

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

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 显示的信息仍然存在,但格式化程序不见了。它可能存在于旧版本中。

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

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

【讨论】:

  • 这是否意味着新版本放弃了“最好的 3”概念?我找到了this thread on github from 2016。好吧,我可以接受这个新概念,尽管我认为它不太准确。如果他们包含一个选项来显示哪些信息,那就太好了。您清楚地证明了信息就在那里。
  • 有人要求选择显示,github.com/ipython/ipython/issues/10712
【解决方案2】:

此语法 std/mean/dev 已于 2016 年 10 月 5 日添加到 IPython。有关此改进,请参阅问题 #9984 https://github.com/ipython/ipython/pull/9984。实现在这里:

“在 %timeit 魔法中添加了 mean + stdv - 新测试待定 -”

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

但语法“best of 3”来自 main() 函数中的 python timeit 模块,这在 python2 和 3 中通常相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多