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

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

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

Termininja
  • 6,620
  • 12
  • 48
  • 49

6 Answers6

50

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

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
12

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

Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
6

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.

Adam Ralph
  • 29,453
  • 4
  • 60
  • 67
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.

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.

abelenky
  • 63,815
  • 23
  • 109
  • 159
2

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

Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
-1

Yes, they are equivalent code snippets.

Servy
  • 202,030
  • 26
  • 332
  • 449
crauscher
  • 6,528
  • 14
  • 59
  • 85