c#中的for和while循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/552766/
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-08-04 07:53:28  来源:igfitidea点击:

for and while loop in c#

c#for-loopwhile-loop

提问by Ashutosh Singh-MVP SharePoint

for (i=0 ; i<=10; i++)
{
    ..
    ..
}

i=0;
while(i<=10)
{
    ..
    ..
    i++;
}

In for and while loop, which one is better, performance wise?

在 for 和 while 循环中,哪个更好,性能明智?

采纳答案by Marc Gravell

(update) Actually - there is one scenario where the forconstruct is more efficient; looping on an array. The compiler/JIT has optimisations for this scenario as long as you usearr.Lengthin the condition:

(更新)实际上 - 在一种情况下,for构造更有效;在数组上循环。只要您arr.Length在条件中使用编译器/JIT 就会针对这种情况进行优化:

for(int i = 0 ; i < arr.Length ; i++) {
    Console.WriteLine(arr[i]); // skips bounds check
}

In this very specific case, it skips the bounds checking, as it already knows that it will never be out of bounds. Interestingly, if you "hoist" arr.Lengthto try to optimize it manually, you prevent this from happening:

在这种非常特殊的情况下,它跳过边界检查,因为它已经知道它永远不会越界。有趣的是,如果您“提升”arr.Length以尝试手动优化它,您可以防止这种情况发生:

int len = arr.Length;
for(int i = 0 ; i < len ; i++) {
    Console.WriteLine(arr[i]); // performs bounds check
}

However, with other containers (List<T>etc), hoisting is fairly reasonable as a manual micro-optimisation.

然而,对于其他容器(List<T>等),提升作为手动微优化是相当合理的。

(end update)

(结束更新)



Neither; a for loop is evaluated as a while loop under the hood anyway.

两者都不; 无论如何,for 循环在引擎盖下被评估为 while 循环。

For example 12.3.3.9 of ECMA 334 (definite assignment) dictates that a for loop:

例如,ECMA 334(明确分配)的 12.3.3.9 规定了 for 循环:

for ( for-initializer ; for-condition ; for-iterator ) embedded-statement

is essentially equivalent (from a Definite assignmentperspective (not quite the same as saying "the compiler must generate this IL")) as:

本质上是等价的(从明确赋值的角度来看(与说“编译器必须生成这个 IL”不完全相同))为:

{
    for-initializer ;
    while ( for-condition ) {
        embedded-statement ;
        LLoop:
        for-iterator ;
    }
}

with continue statements that target the for statement being translated to goto statements targeting the label LLoop. If the for-condition is omitted from the for statement, then evaluation of definite assignment proceeds as if for-condition were replaced with true in the above expansion.

将针对 for 语句的 continue 语句转换为针对标签 LLoop 的 goto 语句。如果 for 条件从 for 语句中省略,则确定赋值的评估继续进行,就好像 for-condition 在上述扩展中被替换为 true。

Now, this doesn't mean that the compiler has to do exactly the same thing, but in reality it pretty much does...

现在,这并不意味着编译器必须做完全相同的事情,但实际上它几乎可以......

回答by Ferdinand Beyer

Neither one. They are equivalent. You can think of the 'for' loop being a more compact way of writing the while-loop.

没有一个。它们是等价的。您可以将“for”循环视为编写 while 循环的更紧凑的方式。

回答by Alex Reitbort

I would say they are the same and you should never do such micro-optimizations anyway.

我会说它们是相同的,无论如何你都不应该做这样的微优化。

回答by crauscher

Yes, they are equivalent code snippets.

是的,它们是等效的代码片段。

回答by Adam Ralph

The performance will be the same. However, unless you need to access the ivariable outside the loop then you should use the forloop. This will be cleaner since iwill only have scope within the block.

性能将是相同的。但是,除非您需要访问i循环外的变量,否则您应该使用for循环。这将更清晰,因为i它只在块内有作用域。

回答by abelenky

Program efficiency comes from proper algorithms, good object-design, smart program architecture, etc.

程序效率来自适当的算法、良好的对象设计、智能程序架构等。

Shaving a cycle or two with for loops vs while loops will NEVER make a slow program fast, or a fast program slow.

使用 for 循环与 while 循环减少一两个循环永远不会使慢程序变快,或使快速程序变慢。

If you want to improve program performance in this section, find a way to either partially unroll the loop (see Duff's Device), or improve performance of what is done inside the loop.

如果您想在本节中提高程序性能,请找到一种方法来部分展开循环(请参阅Duff 的设备),或者提高循环内部执行的性能。