for and while loop in c#
Solution 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(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...
Solution 2
I would say they are the same and you should never do such micro-optimizations anyway.
Solution 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.
Solution 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.
Solution 5
Neither one. They are equivalent. You can think of the 'for' loop being a more compact way of writing the while-loop.
Related videos on Youtube
Ashutosh Singh-MVP SharePoint
Ashutosh is a SharePoint enthusiast from NOIDA, India. With 7 years of experience in .net technologies he specializes in Architecting SharePoint solutions. He is a technical lead at Brickred Technologies. Ashutosh enjoys reading technical articles and so in his free times he could be easily found working on his blog site (www.sharepointworld.in) or technical forums. You can often find him in the SharePoint MSDN forums and stackoverflow trying to help others solve their SharePoint problems. @ashutosh80googlefacebooklinkedin
Updated on July 09, 2022Comments
-
Ashutosh Singh-MVP SharePoint almost 2 years
for (i=0 ; i<=10; i++) { .. .. } i=0; while(i<=10) { .. .. i++; }
In for and while loop, which one is better, performance wise?
-
Krythic almost 8 yearsCan you provide sources and citations for hoisting length breaks optimization?
-
Kevin Streicher about 6 years@Krythic just stumbled across this: codeblog.jonskeet.uk/2009/01/29/… blog post from Jon Skeet.
-
Krythic about 6 yearsNoxMortem Thanks
-
Shubham over 3 yearscode2night.com/Tutorials/Article/Csharp-Tutorials you can learn through the link. in this blog the explanation is so clear