3

Microsoft document: "Code Review: .NET Application Performance" (http://msdn.microsoft.com/en-us/library/ff647802.aspx) recommends "for" loop instead of foreach loop to improve the performance.

Do You Use foreach?

Using foreach can result in extra overhead because of the way enumeration is implemented in .NET Framework collections. .NET Framework 1.1 collections provide an enumerator for the foreach statement to use by overriding the IEnumerable.GetEnumerator.
...
Consider using a for loop instead of foreach to increase performance for iterating through .NET Framework collections that can be indexed with an integer.

My application does expensive call every time user click on a row in grid in a loop. It is about 2 million iteration to convert from floating point values to Color that will be visualized in the monitor.

I am not sure to use for loop instead foreach loop because Resharper make a warning in every for loop which can be converter to foreach loop.

1. Why does Resharper Recommend foreach Instead of for Loop?
2. Is it safe to use for loop?

Yohanes Nurcahyo
  • 601
  • 8
  • 19
  • 1) because it's more _generic_ (it'll work with almost any collection and not only collections) but to use it or not (because of performance) it's something you have to decide by yourself (resharper can't be so smart to know how many items your collection has, how often it's called and if it's performance critical or not). 2) yes, it's perfectly safe (but tied to few types of collections and arrays). – Adriano Repetti Nov 25 '13 at 17:05
  • I *think* the compiler will turn a foreach into a for in certain cases anyways, such as if it knows at compile time the thing being foreached over is an array. – MrKWatkins Nov 25 '13 at 17:08
  • 1
    possible duplicate of [Why should I use foreach instead of for (int i=0; i – GazTheDestroyer Nov 25 '13 at 17:10
  • "Resharper make a warning" - is it actually a warning? Or is it in fact a context action (something ReSharper is saying it **can** do for you, *if you want to*) ? – AakashM Nov 26 '13 at 09:24

3 Answers3

2
  • Performace for is faster!
  • Foreach it can be used as Strongly Typed Collections in easy way!
  • Foreach you get a rid of all indices problems, etc
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
2

Since you mention performance concerns of large number of iterations - yes, foreach may be slower than for. You need to measure both versions of the iteration and see if for indeed faster in your particular case for that one critical iteration. If you find that for is faster (in a way that matters to your program) you can suppress R# warning for that particular loop.

Note that foreach does not require to know length of collection in advance. It may be beneficial to turn your incoming data into some sort of streaming sequence (i.e. IEnumerable<T> or Stream or one of reader classes) and process with foreach till you reach the end instead of loading whole sequence first and iterating on result. This is even more important for large amounts of data (i.e. decompressing large image that may not fit in memory).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1
  1. I believe it's because foreach is more declarative and contains less clutter and bookkeeping compared to for. Also as @TimSchmelter said in comments, it's more flexible as it works with more than just indexable collections. It's just a matter of style (and code readability/maintainability), though.

  2. Define "safe". As long as you're iterating through indexable collection (array, list...), for gets the job done.

Honza Brestan
  • 10,637
  • 2
  • 32
  • 43