c#中的for和while循环

时间:2022-09-06 17:10:47
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循环中,哪一个更好,性能明智?

6 个解决方案

#1


(update) Actually - there is one scenario where the for construct is more efficient; looping on an array. The compiler/JIT has optimisations for this scenario as long as you use arr.Length in 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.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 等),提升作为手动微优化是相当合理的。

(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. 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.

使用continue语句,将for语句转换为转换为以标签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...

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

#2


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

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

#3


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. This will be cleaner since i will only have scope within the block.

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

#4


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

#5


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

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

#6


Yes, they are equivalent code snippets.

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

#1


(update) Actually - there is one scenario where the for construct is more efficient; looping on an array. The compiler/JIT has optimisations for this scenario as long as you use arr.Length in 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.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 等),提升作为手动微优化是相当合理的。

(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. 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.

使用continue语句,将for语句转换为转换为以标签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...

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

#2


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

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

#3


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. This will be cleaner since i will only have scope within the block.

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

#4


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

#5


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

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

#6


Yes, they are equivalent code snippets.

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