In .NET, which loop runs faster, 'for' or 'foreach'?

In C#/VB.NET/.NET, which loop runs faster, for or foreach ?

Ever since I read that a for loop works faster than a foreach loop a long time ago I assumed it stood true for all collections, generic collections, all arrays, etc.

I scoured Google and found a few articles, but most of them are inconclusive (read comments on the articles) and open ended.

What would be ideal is to have each scenario listed and the best solution for the same.

For example (just an example of how it should be):

  • for iterating an array of 1000+ strings - for is better than foreach
  • for iterating over IList (non generic) strings - foreach is better than for
  • A few references found on the web for the same:

  • Original grand old article by Emmanuel Schanzer
  • CodeProject FOREACH Vs. FOR
  • Blog - To foreach or not to foreach , that is the question
  • ASP.NET forum - NET 1.1 C# for vs foreach
  • [Edit]

    Apart from the readability aspect of it, I am really interested in facts and figures. There are applications where the last mile of performance optimization squeezed do matter.


    Patrick Smacchia blogged about this last month, with the following conclusions:

  • for loops on List are a bit more than 2 times cheaper than foreach loops on List.
  • Looping on array is around 2 times cheaper than looping on List.
  • As a consequence, looping on array using for is 5 times cheaper than looping on List using foreach (which I believe, is what we all do).

  • foreach loops demonstrate more specific intent than for loops .

    Using a foreach loop demonstrates to anyone using your code that you are planning to do something to each member of a collection irrespective of its place in the collection. It also shows you aren't modifying the original collection (and throws an exception if you try to).

    The other advantage of foreach is that it works on any IEnumerable , where as for only makes sense for IList , where each element actually has an index.

    However, if you need to use the index of an element, then of course you should be allowed to use a for loop. But if you don't need to use an index, having one is just cluttering your code.

    There are no significant performance implications as far as I'm aware. At some stage in the future it might be easier to adapt code using foreach to run on multiple cores, but that's not something to worry about right now.


    First, a counter-claim to Dmitry's answer. For arrays, the C# compiler emits largely the same code for foreach as it would for an equivalent for loop. That explains why for this benchmark, the results are basically the same:

    using System;
    using System.Diagnostics;
    using System.Linq;
    
    class Test
    {
        const int Size = 1000000;
        const int Iterations = 10000;
    
        static void Main()
        {
            double[] data = new double[Size];
            Random rng = new Random();
            for (int i=0; i < data.Length; i++)
            {
                data[i] = rng.NextDouble();
            }
    
            double correctSum = data.Sum();
    
            Stopwatch sw = Stopwatch.StartNew();
            for (int i=0; i < Iterations; i++)
            {
                double sum = 0;
                for (int j=0; j < data.Length; j++)
                {
                    sum += data[j];
                }
                if (Math.Abs(sum-correctSum) > 0.1)
                {
                    Console.WriteLine("Summation failed");
                    return;
                }
            }
            sw.Stop();
            Console.WriteLine("For loop: {0}", sw.ElapsedMilliseconds);
    
            sw = Stopwatch.StartNew();
            for (int i=0; i < Iterations; i++)
            {
                double sum = 0;
                foreach (double d in data)
                {
                    sum += d;
                }
                if (Math.Abs(sum-correctSum) > 0.1)
                {
                    Console.WriteLine("Summation failed");
                    return;
                }
            }
            sw.Stop();
            Console.WriteLine("Foreach loop: {0}", sw.ElapsedMilliseconds);
        }
    }
    

    Results:

    For loop: 16638
    Foreach loop: 16529
    

    Next, validation that Greg's point about the collection type being important - change the array to a List<double> in the above, and you get radically different results. Not only is it significantly slower in general, but foreach becomes significantly slower than accessing by index. Having said that, I would still almost always prefer foreach to a for loop where it makes the code simpler - because readability is almost always important, whereas micro-optimisation rarely is.

    链接地址: http://www.djcxy.com/p/5398.html

    上一篇: 什么时候装配比C快?

    下一篇: 在.NET中,哪个循环运行得更快,“for”还是“foreach”?