timeit 模块 - 获得最快和最慢的循环

timeit module - get fastest and slowest loop

我想用 Python 的 timeit 模块做一些基准测试。有没有办法让 timeit return 成为 1000 次循环中最慢和最快循环的时间?

代码很容易破解。您应该可以复制 timeit.py source code,将其保存到 my_timeit.py,稍微编辑一下,然后将其用作新模块。

您可以在 line 326 上看到 timeit.py 可以 best = min(r);您可以添加 worst = max(r) 并编辑下面的打印语句。

使用repeat函数代替timeit,其中return一个时间列表。

timeit returns "best of 3" 即有两个参数:一个指定循环中的迭代次数,另一个指定循环次数重复循环。传递给 min() 的结果是每次循环 的时间 ,而不是每次循环迭代的时间。

重复循环的目的是排除同一系统上其他进程的影响——来自文档(help('timeit')):

The best thing to do when accurate timing is necessary is to repeat the timing a few times and use the best time. The -r option is good for this; the default of 3 repetitions is probably enough in most cases.

重复测量1000次没有意义。您可能打算为单个循环指定 1000 次迭代(默认值为 1000000)。

只有最快的循环(最短时间)才有用 -- 来自 help('timeit.Timer.repeat'):

Note: it's tempting to calculate mean and standard deviation from the result vector and report these. However, this is not very useful. In a typical case, the lowest value gives a lower bound for how fast your machine can run the given code snippet; higher values in the result vector are typically not caused by variability in Python's speed, but by other processes interfering with your timing accuracy. So the min() of the result is probably the only number you should be interested in. After that, you should look at the entire vector and apply common sense rather than statistics.emphasis is mine

即,最慢的循环表示有多少其他进程可以干扰测量。

#!/usr/bin/env python
import timeit

def your_function():
    "do something"

t = timeit.Timer(your_function)
# repeat 10 times, 1000000 times through the loop
repeat, number = 10, 1000000
r = t.repeat(repeat, number) 
best, worse = min(r), max(r)
print("{number} loops, best of {repeat}: {best:.3g} seconds per loop, "
     "worse of {repeat}: {worse:.3g} seconds per loop".format(**vars()))

如何使用timeit.repeat

res = timeit.repeat('"-".join(str(n) for n in range(1000))', number=1, repeat=10)

fastest = min(res)
slowest = max(res)

如果您希望每次重复执行多次(比如 n),请不要忘记将结果除以 n

n = 100
res = timeit.repeat('"-".join(str(n) for n in range(1000))', number=n, repeat=10)

fastest = min(res)/n
slowest = max(res)/n

print(fastest, slowest)
# Out:
# 0.00020186356006888674 0.0002322985901264474