ipython /关于百分比循环过程的问题

发布于 2025-02-10 08:13:10 字数 357 浏览 3 评论 0原文

这是我在Google Colab中的代码:

myArray=[]

然后

%%timeit -n 2
myArray.append("1")

结果给出:

“在此处输入图像说明”

我不太了解(我期望MyArray只有两个值)

Here is my code in Google Colab :

myArray=[]

Then

%%timeit -n 2
myArray.append("1")

The resultat gives :

enter image description here

Which I don't really understand (I was expecting only two values for myArray)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2025-02-17 08:13:10

TimeIt有两个参数,您可以通过以下方式来说明应运行多少次:number(-n)和重复(-r)。

  • 重复告诉TimeIt,该示例应采用多少个示例,
  • 指定现在每个样本重复代码的次数

,默认重复值为5。因此,当数字为2和重复为5,2*5 = 10,代码实际运行的次数以及附加到列表的元素数量。

要解决此问题,您还应使用-R指定重复参数。

编辑

您获取的每个示例(-R),您还运行可能已将其传递给TimeIT的设置代码。另一方面,数字(-n)告诉时间仪,它应该为每个示例运行代码多少次。您的代码仅在设置后才执行n时间,该设置完成了r次,每个示例一次。

从TimeIt文档中:

timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000, globals=None)

这是TimeIt函数签名。如您所见,有一个设置参数可以传递以指定在执行代码之前应运行的任何代码(该代码将执行numbern times,等同于-n)在样本中。

现在,让我们将其与timeit.repeat函数进行比较:

timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=5, number=1000000, globals=None)

如您所见,这里有一个额外的参数:重复。请注意,重复的默认值(相当于-R)为5,这就是为什么您在示例中将10个元素附加到列表中的原因。

为什么要同时使用-r-n

最好指定每个样本的运行次数,以及出于可比性原因所需的样品。在执行Python脚本时,请考虑每个示例:Python必须加载脚本,执行一些初始设置,然后才能运行您的代码。
您还可以将number-n)视为 for 循环中的迭代次数:该设置已经在之前完成运行您的代码。

这是您使用timeit模块时发生的类似Python的伪代码表示:

def timeit(your_code, repeat, number, setup): 
    for r in range(repeat):
        perform_setup()
        for n in range(number):
            run(your_code)

希望这会有所帮助。

Timeit has two arguments you can pass to tell it how many times the code should be run: number (-n) and repeat (-r).

  • repeat tells timeit how many samples it should take
  • number specifies the number of times to repeat the code for each sample

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 executed n times only after the setup, which is done r times, once for every sample.

From the timeit documentation:

timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000, globals=None)

This is the timeit function signature. As you can see, there is a setup parameter you can pass to specify any code that should be run prior to executing your code (which will be executed number times, equivalent to -n) in the sample.

Let's now compare it with the timeit.repeat function:

timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=5, number=1000000, globals=None)

As you can see, there is an extra parameter here: repeat. Note that the default value of repeat (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 a for 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:

def timeit(your_code, repeat, number, setup): 
    for r in range(repeat):
        perform_setup()
        for n in range(number):
            run(your_code)

Hope this helps, cheers.

一念一轮回 2025-02-17 08:13:10

TimeIt具有循环和运行。您刚刚指定了每次运行的循环。

> In [80]: alist = []    
In [81]: %%timeit
    ...: alist.append(1)
    ...: 
    ...: 
146 ns ± 12.6 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [82]: len(alist)
Out[82]: 81111111

In [83]: alist=[]    
In [84]: %%timeit -n2 -r1
    ...: alist.append(1)
    ...: 
    ...: 
1.75 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)

In [85]: len(alist)
Out[85]: 2

In [86]: alist=[]    
In [87]: %%timeit -n2
    ...: alist.append(1)
    ...: 
    ...: 
993 ns ± 129 ns per loop (mean ± std. dev. of 7 runs, 2 loops each)

In [88]: len(alist)
Out[88]: 14

通常,我尝试设置一个时间IT,因此我不在乎“结果”是什么,因为我想要时间,而不是某种累积的列表或数组。

对于新清单,每个运行

In [89]: %%timeit -n2  -r10 alist=[]
    ...: alist.append(1)
    ...: 
    ...: 
The slowest run took 4.75 times longer than the fastest. This could mean that an intermediate result is being cached.
1.21 µs ± 889 ns per loop (mean ± std. dev. of 10 runs, 2 loops each)

timeit has loops and runs. You just specified the loops per run.

> In [80]: alist = []    
In [81]: %%timeit
    ...: alist.append(1)
    ...: 
    ...: 
146 ns ± 12.6 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [82]: len(alist)
Out[82]: 81111111

In [83]: alist=[]    
In [84]: %%timeit -n2 -r1
    ...: alist.append(1)
    ...: 
    ...: 
1.75 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 2 loops each)

In [85]: len(alist)
Out[85]: 2

In [86]: alist=[]    
In [87]: %%timeit -n2
    ...: alist.append(1)
    ...: 
    ...: 
993 ns ± 129 ns per loop (mean ± std. dev. of 7 runs, 2 loops each)

In [88]: len(alist)
Out[88]: 14

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:

In [89]: %%timeit -n2  -r10 alist=[]
    ...: alist.append(1)
    ...: 
    ...: 
The slowest run took 4.75 times longer than the fastest. This could mean that an intermediate result is being cached.
1.21 µs ± 889 ns per loop (mean ± std. dev. of 10 runs, 2 loops each)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文