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

前端 未结 30 1334
抹茶落季
抹茶落季 2020-11-22 04:25

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

相关标签:
30条回答
  • 2020-11-22 04:39

    First, a counter-claim to Dmitry's (now deleted) 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.

    0 讨论(0)
  • 2020-11-22 04:39

    I wouldn't expect anyone to find a "huge" performance difference between the two.

    I guess the answer depends on the whether the collection you are trying to access has a faster indexer access implementation or a faster IEnumerator access implementation. Since IEnumerator often uses the indexer and just holds a copy of the current index position, I would expect enumerator access to be at least as slow or slower than direct index access, but not by much.

    Of course this answer doesn't account for any optimizations the compiler may implement.

    0 讨论(0)
  • 2020-11-22 04:42

    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.

    0 讨论(0)
  • 2020-11-22 04:45

    It will always be close. For an array, sometimes for is slightly quicker, but foreach is more expressive, and offers LINQ, etc. In general, stick with foreach.

    Additionally, foreach may be optimised in some scenarios. For example, a linked list might be terrible by indexer, but it might be quick by foreach. Actually, the standard LinkedList<T> doesn't even offer an indexer for this reason.

    0 讨论(0)
  • 2020-11-22 04:46

    This should save you:

    public IEnumerator<int> For(int start, int end, int step) {
        int n = start;
        while (n <= end) {
            yield n;
            n += step;
        }
    }
    

    Use:

    foreach (int n in For(1, 200, 4)) {
        Console.WriteLine(n);
    }
    

    For greater win, you may take three delegates as parameters.

    0 讨论(0)
  • 2020-11-22 04:46

    The two will run almost exactly the same way. Write some code to use both, then show him the IL. It should show comparable computations, meaning no difference in performance.

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