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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:07:09  来源:igfitidea点击:

Why does %timeit loop different number of times?

pythonpandasipythonjupyter-notebookjupyter

提问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 上,我试图比较两种方法之间用于查找具有最大值的索引所花费的时间。

enter image description here

在此处输入图片说明

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

%timeitlibrary 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

It has a built-in option -n: " Options: -n: execute the given statement times in a loop. If this value is not given, a fitting value is chosen."docs

它有一个内置选项 -n:“选项:-n:在循环中执行给定的语句次数。如果没有给出这个值,则选择一个合适的值。” 文档

So it choses the number of loops itself if not specified.

因此,如果未指定,它会自行选择循环数。

回答by M?ltus

use -rto 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)