python - timeit.timeit的返回值是平均值还是最佳值?

标签 python performance

doc清楚地告诉我们 timeit 命令行界面输出最好。

python -m timeit '"-".join(map(str, range(100)))'
10000 loops, best of 3: 25.2 usec per loop

Python 接口(interface)怎么样?

>>> import timeit
>>> timeit.timeit('char in text', setup='text = "sample string"; char = "g"')
0.41440500499993504

它仍然是最好的吗?

最佳答案

命令行将repeat选项设置为3:

-r N, --repeat=N

how many times to repeat the timer (default 3)

“the best”是这 3 个中最好的。这与 number 参数不同,当您不设置 -n/- 时,该参数会自动为您确定-number 参数。

另一方面,timeit.timeit() 函数不重复。它运行语句次数次,并给出总时间。来自 timeit.timeit() documentation :

Create a Timer instance with the given statement, setup code and timer function and run its timeit() method with number executions.

来自timeit.Timer.timeit() :

Time number executions of the main statement. This executes the setup statement once, and then returns the time it takes to execute the main statement a number of times, measured in seconds as a float.

如果您想获得最佳结果,请使用 timeit.repeat() function :

 timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000)

Create a Timer instance with the given statement, setup code and timer function and run its repeat() method with the given repeat count and number executions.

但是,使用timeit.repeat()不会为您自动调整数字范围。为此,您必须创建自己的 Timer() 实例。

文档链接到 implementation ,因此您可以在 main() 函数中看到这是如何完成的;将其简化为使用默认选项时执行的代码:

t = Timer(stmt, setup, timer)
repeat = 3
for i in range(1, 10):
    number = 10**i
    x = t.timeit(number)
    if x >= 0.2:
        break
r = t.repeat(repeat, number)
best = min(r)
print "%d loops," % number,
usec = best * 1e6 / number
if usec < 1000:
    print "best of %d: %.*g usec per loop" % (repeat, 3, usec)
else:
    msec = usec / 1000
    if msec < 1000:
        print "best of %d: %.*g msec per loop" % (repeat, 3, msec)
    else:
        sec = msec / 1000
        print "best of %d: %.*g sec per loop" % (repeat, 3, sec)

在 Python 3 中,新的 Timer.autorange() method 对上述内容进行了很大改进。以及更好地处理秤。

使用您的语句和设置进行演示:

>>> import timeit
>>> t = timeit.Timer('char in text', setup='text = "sample string"; char = "g"')
>>> repeat = 3
>>> for i in range(1, 10):
...     number = 10**i
...     x = t.timeit(number)
...     if x >= 0.2:
...         break
...
>>> r = t.repeat(repeat, number)
>>> best = min(r)
>>> print "%d loops," % number,
10000000 loops,
>>> usec = best * 1e6 / number
>>> if usec < 1000:
...     print "best of %d: %.*g usec per loop" % (repeat, 3, usec)
... else:
...     msec = usec / 1000
...     if msec < 1000:
...         print "best of %d: %.*g msec per loop" % (repeat, 3, msec)
...     else:
...         sec = msec / 1000
...         print "best of %d: %.*g sec per loop" % (repeat, 3, sec)
...
best of 3: 0.0305 usec per loop

关于python - timeit.timeit的返回值是平均值还是最佳值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57056958/

相关文章:

performance - Windows Azure 表存储 LINQ 运算符

python - 权限错误 : [WinError 32] The process cannot access the file because it is being used by another process:

python - 使用我的号码从 Twilio 发送 Whatsapp 消息

Python MySQL-Connector 需要一分多钟才能打开连接

python - 为什么我的 linkExtractor 在 scrapy 蜘蛛中伪装成不抓取允许的链接?

javascript - 大部分数据集中在 1 个对象中的优点/缺点

c# - 将方法/属性标记为虚拟的性能影响是什么?

python - 在 Django 中将多个值从 url 转换为 json

python - token 扩展与匹配器与短语匹配器与 spaCy 中的实体标尺

performance - Jmeter 性能测试 DOM 和加载时间