python - 为什么 python 的 timeit 使用 'best of 3' 来测量耗时?

标签 python benchmarking microbenchmark timeit

我不明白为什么 python 的 timeit 模块使用 best of 3 来测量时间。这是我的控制台中的示例:

~ python -m timeit 'sum(range(10000))'
10000 loops, best of 3: 119 usec per loop

凭直觉,我会将所有时间放在一起,然后除以循环次数。在所有循环中选择最好的 3 个的直觉是什么?这似乎有点不公平。

最佳答案

the documentation 中所述:

default_timer() measurations can be affected by other programs running on the same machine, so 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.

关于python - 为什么 python 的 timeit 使用 'best of 3' 来测量耗时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34499747/

相关文章:

python - Pandas 不根据列值进行过滤

Python - 还有其他方法可以在列表理解中应用函数和过滤器吗?

c - 指针追踪在此基准测试中的工作方式

c++ - 基准可变参数模板函数调用

PHP 微基准测试(变量和条件语句)

python - 如何在Python中循环三个变量,同时它们的总和始终等于1?

python - 使用 sqlalchemy orm 时使用 postgresql 服务器端游标

c++ - Google Benchmark 自定义设置和拆卸方法

php - TTFB - 跟踪 php 函数的运行时间

java - 为什么 volatile 比非 volatile 工作得更快?