python: 慢 timeit() 函数

标签 python timer timeit

当我在 timeit() 之外运行下面的代码时,它似乎立即完成。但是,当我在 timeit() 函数中运行它时,它需要更长的时间。为什么?

>>> import timeit
>>> t = timeit.Timer("3**4**5")
>>> t.timeit()
16.55522028637718

使用: Python 3.1 (x86) - AMD 速龙 64 X2 - WinXP(32 位)

最佳答案

timeit() 函数多次运行代码(默认为 100 万次)并取平均时间。

要只运行一次代码,请执行以下操作:

t.timeit(1)

但这会给您带来偏差的结果 - 它会重复出现。

要获得让它重复的每循环时间,请将结果除以循环数。如果一百万太多,请使用较小的重复次数值:

count = 1000
print t.timeit(count) / count

关于python: 慢 timeit() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1257727/

相关文章:

python - Django ORM 查询性能

python - 在 Python 中为形状赋值

python - 是否有更短/更好的方法来验证请求参数?

Java程序签到定时器

php - 如何在 PHP 中设置定时器?

python - IPython:如何为每次迭代保存 timeit 值

python - Boost - 无法包装传入变量

python - 在 python 中检查区分大小写的 os.path.isfile(filename)

c# - DispatcherTimer 与 Thread.Sleep 的效率对比

ipython - 你能捕捉到 ipython 魔法方法的输出吗? (时间)