for and while loop in c#

前端 未结 6 1808
长情又很酷
长情又很酷 2020-12-03 05:22
for (i=0 ; i<=10; i++)
{
    ..
    ..
}

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

In for and while loop, which one is better, performanc

相关标签:
6条回答
  • 2020-12-03 05:33

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

    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.

    (end update)


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

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

    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:

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

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

    0 讨论(0)
  • 2020-12-03 05:35

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

    0 讨论(0)
  • 2020-12-03 05:37

    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.

    0 讨论(0)
  • 2020-12-03 05:42

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

    0 讨论(0)
  • 2020-12-03 05:43

    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.

    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.

    0 讨论(0)
  • 2020-12-03 05:51

    Yes, they are equivalent code snippets.

    0 讨论(0)
提交回复
热议问题