ipython /关于百分比循环过程的问题
Here is my code in Google Colab :
myArray=[]
Then
%%timeit -n 2
myArray.append("1")
The resultat gives :
Which I don't really understand (I was expecting only two values for myArray)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TimeIt有两个参数,您可以通过以下方式来说明应运行多少次:number(
-n
)和重复(-r
)。,默认重复值为5。因此,当数字为2和重复为5,2*5 = 10,代码实际运行的次数以及附加到列表的元素数量。
要解决此问题,您还应使用
-R
指定重复参数。编辑
您获取的每个示例(
-R
),您还运行可能已将其传递给TimeIT的设置代码。另一方面,数字(-n
)告诉时间仪,它应该为每个示例运行代码多少次。您的代码仅在设置后才执行n
时间,该设置完成了r
次,每个示例一次。从TimeIt文档中:
这是
TimeIt
函数签名。如您所见,有一个设置
参数可以传递以指定在执行代码之前应运行的任何代码(该代码将执行numbern
times,等同于-n
)在样本中。现在,让我们将其与
timeit.repeat
函数进行比较:如您所见,这里有一个额外的参数:
重复
。请注意,重复
的默认值(相当于-R
)为5,这就是为什么您在示例中将10个元素附加到列表中的原因。为什么要同时使用
-r
和-n
?最好指定每个样本的运行次数,以及出于可比性原因所需的样品。在执行Python脚本时,请考虑每个示例:Python必须加载脚本,执行一些初始设置,然后才能运行您的代码。
您还可以将
number
(-n
)视为 for 循环中的迭代次数:该设置已经在之前完成运行您的代码。这是您使用
timeit
模块时发生的类似Python的伪代码表示:希望这会有所帮助。
Timeit has two arguments you can pass to tell it how many times the code should be run: number (
-n
) and repeat (-r
).Now, the default repeat value is 5. So, when number is 2 and repeat is 5, 2*5=10, which is the number of times the code is actually run and also the number of elements that get appended to the list.
To fix this you should also specify the repeat argument with
-r
.Edit
For every sample you take (
-r
), you also run the setup code you may have passed to timeit. On the other hand, the number (-n
) tells timeit how many times it should run your code for every sample. Your code is executedn
times only after the setup, which is doner
times, once for every sample.From the timeit documentation:
This is the
timeit
function signature. As you can see, there is asetup
parameter you can pass to specify any code that should be run prior to executing your code (which will be executednumber
times, equivalent to-n
) in the sample.Let's now compare it with the
timeit.repeat
function:As you can see, there is an extra parameter here:
repeat
. Note that the default value ofrepeat
(equivalent to-r
) is 5, and this is why you get 10 elements appended to your list in your example.Why should you use both
-r
and-n
?It's better to specify both the number of runs per sample, as well as the samples to take for comparability reasons. Think of every sample as you executing your Python script: Python has to load the script, perform some initial setup, and only then does it run your code.
You can also think of the
number
(-n
) as the number of iterations in afor
loop: the setup has already been done prior to running your code.Here's a simplified Python-like pseudocode representation of what's happening when you use the
timeit
module:Hope this helps, cheers.
TimeIt
具有循环和运行。您刚刚指定了每次运行的循环。通常,我尝试设置一个时间IT,因此我不在乎“结果”是什么,因为我想要时间,而不是某种累积的列表或数组。
对于新清单,每个
运行
:timeit
has loops and runs. You just specified the loops per run.Generally I try to setup a timeit so I don't care what the "result" is, since I want the times, not some sort of accumulated list or array.
For a fresh list each
run
: