pandas 为什么 %timeit 循环不同的次数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45374921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does %timeit loop different number of times?
提问by Bharath
On Jupter Notebook, i was trying to compare time taken between the two methods for finding the index with max value.
在 Jupter Notebook 上,我试图比较两种方法之间用于查找具有最大值的索引所花费的时间。
In the Image, the first function took, 1000 loops, and the second took 10000 loops, is this increase in loops due to the method itself OR Jupyter Just added more loops to get more accurate time per loop even though the second function maybe took 1000 only, is that the case?
在图像中,第一个函数进行了 1000 次循环,第二次进行了 10000 次循环,这是由于方法本身造成的循环增加还是 Jupyter 只是添加了更多循环以获得每个循环更准确的时间,即使第二个函数可能需要 1000只是,是这样吗?
回答by Bharath
%timeit
library will limit the number of runs depending on how long the script takes to execute.
%timeit
库将根据脚本执行所需的时间来限制运行次数。
The number of runs may be set with -n. Example:
可以使用 -n 设置运行次数。例子:
%timeit -n 5000
df = pd.DataFrame({'High':[1,4,8,4,0]})
5000 loops, best of 3: 592 μs per loop
回答by Krishal
回答by M?ltus
use -r
to limit the number of run's:
用于-r
限制运行次数:
import time
%timeit -r1 time.sleep(2)
# 2 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)
%timeit -r4 time.sleep(2)
# 2 s ± 800 μs per loop (mean ± std. dev. of 4 runs, 1 loop each)
%timeit time.sleep(2)
# 2 s ± 46.5 μs per loop (mean ± std. dev. of 7 runs, 1 loop each)