简体   繁体   English

c#中的for和while循环

[英]for and while loop in c#

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 循环中,哪个更好,性能明智?

(update) Actually - there is one scenario where the for construct is more efficient; (更新)实际上 - 在一种情况下for构造更有效; looping on an array.在数组上循环。 The compiler/JIT has optimisations for this scenario as long as you use arr.Length in the condition :只要您在条件中使用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.Length to 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 assignment perspective (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.将针对 for 语句的 continue 语句转换为针对标签 LLoop 的 goto 语句。 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 条件从 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...现在,这并不意味着编译器必须做完全相同的事情,但实际上它几乎可以......

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

The performance will be the same.性能将是相同的。 However, unless you need to access the i variable outside the loop then you should use the for loop.但是,除非您需要在循环外访问i变量,否则您应该使用for循环。 This will be cleaner since i will only have scope within the block.这会更干净,因为i只会在块内有范围。

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 的设备),或者提高循环内部执行的性能。

Neither one.没有一个。 They are equivalent.它们是等价的。 You can think of the 'for' loop being a more compact way of writing the while-loop.您可以将“for”循环视为编写 while 循环的更紧凑的方式。

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

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

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