Compare loop performance in C#

[loop performance]

The Code Project post FOREACH Vs. FOR (C#) examines the IL code created for a C# foreach loop and a for loop to compare loop performance. The author notices some unnecessary statements in the foreach loop and therefore concludes that it will be slower.

That’s true as far as it goes, but I wanted to see just how big the difference was so I wrote this example to test loop performance. It uses the following code to compare the performance of foreach, for, and while loops. The looping code is highlighted in blue so it’s easy to separate from the timing code.

private void btnGo_Click(object sender, EventArgs e)
{
    Cursor = Cursors.WaitCursor;
    txtForEach.Clear();
    txtFor.Clear();
    txtWhile.Clear();
    txtForEach.Refresh();
    txtFor.Refresh();
    txtWhile.Refresh();

    int num_items = int.Parse(txtArraySize.Text);
    int num_trials = int.Parse(txtNumTrials.Text);
    int[] array = new int[num_items];
    int total = 0;
    Stopwatch watch = new Stopwatch();

    total = 0;
    watch.Start();
    for (int trial = 0; trial < num_trials; trial++)
    {
        foreach (int value in array)
        {
            total += value;
        }
    }
    watch.Stop();
    txtForEach.Text = watch.Elapsed.TotalSeconds.ToString("0.00");
    txtForEach.Refresh();

    total = 0;
    watch.Reset();
    watch.Start();
    for (int trial = 0; trial < num_trials; trial++)
    {
        for (int i = 0; i < num_items; i++)
        {
            total += array[i];
        }
    }
    watch.Stop();
    txtFor.Text = watch.Elapsed.TotalSeconds.ToString("0.00");
    txtFor.Refresh();

    total = 0;
    watch.Reset();
    watch.Start();
    for (int trial = 0; trial < num_trials; trial++)
    {
        int i = 0;
        while (i < num_items)
        {
            total += array[i++];
        }
    }
    watch.Stop();
    txtWhile.Text = watch.Elapsed.TotalSeconds.ToString("0.00");
    txtWhile.Refresh();

    Cursor = Cursors.Default;
}

The picture at the top of this post shows that foreach was the slowest and for was fastest as predicted by the Code Project article. The while loop is basically a clumsy for loop so it has slightly slower performance.

[loop performance]

At least in Visual Studio 2008. The picture on the right shows the same program running in Visual Studio 2015. Now the foreach loop is fastest, the while loop takes second place, and the for loop comes in third.

I want to make two points here. First, you can’t depend on the exact timing of different language constructs because the language team may change they way are implemented in different versions. In this case Visual Studio 2015 implements the foreach and for differently than Visual Studio 2008 does so the timings changed. If you rely on one set of behaviors, you may be disappointed if it changes later.

The second point is that the differences in these speeds is ludicrously small. In the Visual Studio 2015 program, the total difference was 0.11 seconds but that was over 100 million iterations through the loops. That means the difference is only 0.0000000011 seconds per iteration. Unless you plan on running a HUGE number of iterations, the difference won’t matter.

In fact a real program would probably do more than just add up the values inside the loops. In that case the time spent performing those operations would be a much greater fraction of the total time than the tiny difference in loop speed. In other words, if performing a calculation 100 million times takes 10 seconds, wasting 0.11 seconds on a for loop really isn’t a big deal.

The moral is that you should pick the kind of loop that makes the code easiest to read and understand. I’ve never seen a project fail because performance was too slow by a few tenths of a second but I’ve seen several project fail because the code was too hard to read and debug.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in performance, syntax and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *