【发布时间】: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”是更好的衡量标准,所以我想改变我的输出。
我阅读了IPython 和Python 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