简体   繁体   English

是不是比 for 快?

[英]Is while faster than for?

As in the topic, I learnt in school, that loop for is faster than loop while , but someone told me that while is faster.正如我在学校学到的主题一样,循环for比循环while快,但有人告诉我while更快。

I must optimize the program and I want to write while instead for , but I have a concern that it will be slower?我必须优化程序,我想写while不是for ,但我担心它会变慢?

for example I can change for loop:例如我可以更改for循环:

for (int i=0; i<x; i++)
{
   cout<<"dcfvgbh"<<endl;
}

into while loop:进入while循环:

i=0;
while (i<x)
{
    cout<<"dcfvgbh"<<endl;
    i++;
}

The standard requires (§6.5.3/1) that:该标准要求(第 6.5.3/1 节):

The for statement for语句
for ( for-init-statement condition opt ; expression opt ) statement for ( for-init-statement condition opt ; expression opt ) 语句
is equivalent to相当于

{ for-init-statement while ( condition ) { statement expression; } }

As such, you're unlikely to see much difference between them (even if execution time isn't necessarily part of the equivalence specified in the standard).因此,您不太可能看到它们之间的太大差异(即使执行时间不一定是标准中指定的等效项的一部分)。 There are a few exceptions listed to the equivalence as well (scopes of names, execution of the expression before evaluating the condition if you execute a continue ).也列出了一些等价的例外情况(名称的范围,如果执行continue则在评估条件之前执行表达式)。 The latter could, at least theoretically, affect speed a little bit under some conditions, but probably not enough to notice or care about as a rule, and definitely not unless you actually used a continue inside the loop.后者至少在理论上可能会在某些条件下稍微影响速度,但通常可能不足以引起注意或关心,除非您在循环中实际使用了continue否则绝对不会。

For all intents and purposes for is just a fancy way of writing while , so there is no performance advantage either way.出于所有意图和目的, for只是一种奇特的while书写方式,因此无论哪种方式都没有性能优势。 The main reason to use one over the other is how the intent is translated so the reader understands better what the loop is actually doing.使用一个而不是另一个的主要原因是意图如何翻译,以便读者更好地理解循环实际在做什么。

No.没有。

Nope, it's not.不,不是。

It is not faster.它不是更快。

You cout will eat 99% of the clock cycles for this loop.coutcout此循环 99% 的时钟周期。 Beware micro-optimization.当心微观优化。 At any rate, these two will give essentially identical code.无论如何,这两个将给出基本相同的代码。

The only time when a for loop can be faster is when you have a known terminating condition - eg for循环可以更快的唯一时间是当你有一个已知的终止条件 - 例如

for(ii = 0; ii < 24; ii++)

because some optimizing compilers will perform loop unrolling .因为一些优化编译器会执行loop unrolling This means they will not perform a test on every pass through the loop because they can "see" that just doing the thing inside the loop 24 times (or 6 times in blocks of 4, etc) will be a tiny bit more efficient.这意味着他们不会在每次通过循环时都执行测试,因为他们可以“看到”在循环内执行 24 次(或 4 个块中的 6 次等)会更有效一点。 When the thing inside the loop is very small (eg jj += ii ;), such optimization makes the for loop a bit faster than the while (which typically doesn't do "unrolling").当循环内的东西非常小(例如jj += ii ;)时,这种优化会使 for 循环比while快一点(通常不会“展开”)。

Otherwise - no difference.否则 - 没有区别。

update at the request of @zeroth应@zeroth 的要求更新

Source: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.47.9346&rep=rep1&type=pdf资料来源: http : //citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.47.9346&rep=rep1&type=pdf

Quote from source (my emphasis):引用来源(我的重点):

Unrolling a loop at the source- code level involves identification of loop constructs (eg, for, while, do-while, etc.), determination of the loop count to ensure that it is a counting loop, replication of the loop body and the adjustment of loop count of the unrolled loop.在源代码级别展开循环涉及识别循环结构(例如,for、while、do-while 等)、确定循环计数以确保它是一个计数循环、复制循环体和调整展开循环的循环计数。 A prologue or epilogue code may also be inserted.也可以插入序言或结尾代码。 Using this approach, it is difficult to unroll loops formed using a while and goto statements since the loop count is not obvious.使用这种方法,很难展开使用 while 和 goto 语句形成的循环,因为循环计数并不明显。 However, for all but the simplest of loops, this approach is tedious and error prone.然而,对于除最简单的循环之外的所有循环,这种方法既乏味又容易出错。

The other alternative is to unroll loops automatically.另一种选择是自动展开循环。 Automatic unrolling can be done early on source code, late on the unoptimized intermediate representation, or very late on an optimized representation of the program.自动展开可以在源代码的早期完成,在未优化的中间表示的后期完成,或者在程序的优化表示的后期完成。 If it is done at the source-code level, then typically only counting loops formed using for statements are unrolled.如果它是在源代码级别完成的,那么通常只会展开使用 for 语句形成的计数循环。 Unrolling loops formed using other control constructs is difficult since the loop count is not obvious.展开使用其他控制结构形成的循环很困难,因为循环计数并不明显。

To the best of my knowledge swapping out for loops for while loops is not an established optimization technique.据我所知,将for循环换成while循环并不是一种既定的优化技术。

Both your examples will be identical in performance, but as an exercise you could time them to confirm this for yourself.您的两个示例在性能上都是相同的,但作为练习,您可以自己计时来确认这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM