我不明白为什么 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/