philkark
philkark

Reputation: 2457

Calculation time differences in loops

While writing on a program I currently work on, I noticed some change in perfomance and wanted to check the calculation times in some loops I am using to explain this.

I wrote a very simple program that does a simple addition calculation in a loop and outputs the time it took to do it and I noticed two for me at the moment unexplainable effects.

First the code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        int a = 1, b = 2, c = 0;

        Stopwatch sw = new Stopwatch();

        sw.Start();
        for (int i = 0; i < 100; i++)
        {
            c += (a + b);
        }
        sw.Stop();
        c = 0;
        textBlock1.Text = 100 + ": " + sw.ElapsedTicks;

        sw.Restart();
        for (int i = 0; i < 1000; i++)
        {
            c += (a + b);
        }
        sw.Stop();
        c = 0;
        textBlock2.Text = 1000 + ": " + sw.ElapsedTicks;

        sw.Restart();
        for (int i = 0; i < 10000; i++)
        {
            c += (a + b);
        }
        sw.Stop();
        c = 0;
        textBlock3.Text = 10000 + ": " + sw.ElapsedTicks;

        sw.Restart();
        for (int i = 0; i < 1000000; i++)
        {
            c += (a + b);
        }
        sw.Stop();
        c = 0;
        textBlock4.Text = 1000000 + ": " + sw.ElapsedTicks;

        sw.Restart();
        for (int i = 0; i < 100000000; i++)
        {
            c += (a + b);
        }
        sw.Stop();

        textBlock5.Text = 100000000 + ": " + sw.ElapsedTicks;
    }
}

Now I could oberserve the following:

  1. The first time after starting the program clicking on the button and calculating, the short loop of 100 takes around twice/three times (6-9 ticks) as long as the second time I run it (2-4). After that it will remain at around the same length (2-4 ticks).

  2. There are weird proportions between the loop times which remain quite constant when I keep clicking the button which are about as follows:

    • 100: 3
    • 1000: 15
    • 10000: 150
    • 1000000: 17000
    • 100000000: 840000

Is there an explanation why the 100 loop takes aroud 1/5 of the time of the 1000 loop, even though it is 1/10 times as many calculations, and also why does the 100 Mio. loop take only around 50 times as long as the 1 Mio. loop even though it is 100 times as many calculations? And does anybody have an idea why the time (especially for the 100 loop) change after I ran the calculation once after starting the program and then remains quite constant?

Upvotes: 1

Views: 195

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500855

Is there an explanation why the 100 loop takes aroud 1/5 of the time of the 1000 loop, even though it is 1/10 times as many calculations

You're measuring three ticks that's a tiny, tiny amount of time. If you happened to run into a context switch, you'd probably lose much more time than that, even if your thread was scheduled again straight away. It's basically not a large enough amount of time to be measured sensibly, even with Stopwatch.

And does anybody have an idea why the time (especially for the 100 loop) change after I ran the calculation once after starting the program and then remains quite constant?

My guess is that it's to do with JIT compilation and/or garbage collection.

This is why typically benchmarks run a "warmup" round first, and they test a sensible amount of work so that the tests will take a reasonable amount of time, smoothing little bumps in timing.

Upvotes: 3

Related Questions