c#中的for和while循环

Ash*_*int 20 c# for-loop while-loop

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

i=0;
while(i<=10)
{
    ..
    ..
    i++;
}
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ell 47

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

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

在这个非常具体的情况下,它会跳过边界检查,因为它已经知道它永远不会超出范围.有趣的是,如果您"提升" arr.Length以尝试手动优化它,则可以防止这种情况发生:

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

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

(最后更新)


无论; 无论如何,for循环被评估为引擎盖下的while循环.

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

for ( for-initializer ; for-condition ; for-iterator ) embedded-statement
Run Code Online (Sandbox Code Playgroud)

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

{
    for-initializer ;
    while ( for-condition ) {
        embedded-statement ;
        LLoop:
        for-iterator ;
    }
}
Run Code Online (Sandbox Code Playgroud)

with continue语句,将for语句转换为目标标签LLoop的goto语句.如果for语句中省略了for条件,那么对明确赋值的评估就好像for-condition在上面的扩展中被替换为true一样.

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


Ale*_*ort 12

我会说它们是相同的,你绝不应该做这样的微观优化.


Ada*_*lph 6

表现将是相同的.但是,除非您需要i在循环外部访问变量,否则应该使用for循环.这将更清洁,因为i只有块内的范围.