Compare the performance of incrementing statements in C#

[example]

This is another performance testing post. Assembly language usually has statements comparable to x++, x--, x +=, and other operators that add or subtract to a value and store the result in the same value, so it makes some sense that those statements might be faster than statements like x = x + 3 in C#.

This example uses the following code to test the speeds of the statements x = x + 1, x++, x += 1, x = x + 3, and x += 3.

// Perform the trials.
private void btnGo_Click(object sender, EventArgs e)
{
    long num_trials = long.Parse(txtNumTrials.Text);
    lblXPlus1.Text = "";
    lblXIncr.Text = "";
    lblPlusEquals1.Text = "";
    lblPlus3.Text = "";
    lblPlusEquals3.Text = "";
    Cursor = Cursors.WaitCursor;
    Refresh();

    long x;
    Stopwatch watch = new Stopwatch();

    // Trials using x = x + 1.
    x = 1;
    watch.Start();
    for (long i = 0; i < num_trials; i++)
    {
        x = x + 1;
    }
    watch.Stop();
    lblXPlus1.Text = watch.Elapsed.TotalSeconds.
        ToString("0.00") + " seconds";
    lblXPlus1.Refresh();

    // Trials using x++.
    x = 1;
    watch.Reset();
    watch.Start();
    for (long i = 0; i < num_trials; i++)
    {
        x++;
    }
    watch.Stop();
    lblXIncr.Text = watch.Elapsed.TotalSeconds.
        ToString("0.00") + " seconds";
    lblXIncr.Refresh();

    // Trials using x += 1.
    x = 1;
    watch.Reset();
    watch.Start();
    for (long i = 0; i < num_trials; i++)
    {
        x += 1;
    }
    watch.Stop();
    lblPlusEquals1.Text = watch.Elapsed.TotalSeconds.
        ToString("0.00") + " seconds";
    lblPlusEquals1.Refresh();

    // Trials using x = x + 3.
    x = 1;
    watch.Reset();
    watch.Start();
    for (long i = 0; i < num_trials; i++)
    {
        x = x + 3;
    }
    watch.Stop();
    lblPlus3.Text = watch.Elapsed.TotalSeconds.
        ToString("0.00") + " seconds";
    lblPlus3.Refresh();

    // Trials using x += 3.
    x = 1;
    watch.Reset();
    watch.Start();
    for (long i = 0; i < num_trials; i++)
    {
        x += 3;
    }
    watch.Stop();
    lblPlusEquals3.Text = watch.Elapsed.TotalSeconds.
        ToString("0.00") + " seconds";
    lblPlusEquals3.Refresh();

    Cursor = Cursors.Default;
}

The code is straightforward. It simply loops through a series of tests evaluating the desired statements.

If you look closely at the picture above, you’ll see that all of these statements have very nearly the same performance. That means it doesn’t matter which statement you use to perform an operation. Use the one that you find easiest to read and understand and don’t worry about any tiny differences in performance.

Also note that all of these statements are blindingly fast. The difference in speed between the fastest and slowest tests (and these vary when you rerun the tests) is less than a quarter second per billion operations.


Download Example   Follow me on Twitter   RSS feed   Donate




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 *