Compare the performance of the int, long, and byte data types in C#

[performance]

The example Compare the performance of the float, double, and decimal data types in C# compares the performance of the float, double, and decimal data types. This example performs a similar test for the int, long, and byte data types.

When you enter the number of trials that you want to perform and click the Go button, the following code executes.

// Compare performances.
private void btnGo_Click(object sender, EventArgs e)
{
    txtTimeFloat.Clear();
    txtTimeInt.Clear();
    txtTimeLong.Clear();
    txtTimeByte.Clear();
    Cursor = Cursors.WaitCursor;
    Refresh();

    int num_trials = int.Parse(txtNumTrials.Text);
    Stopwatch watch = new Stopwatch();
    float float1, float2, float3;
    int int1, int2, int3;
    long long1, long2, long3;
    byte byte1, byte2, byte3;

    watch.Start();
    int float_trials = num_trials / 10;
    for (int i = 0; i < num_trials; i++)
    {
        float1 = 1.23f;
        float2 = 4.56f;
        float3 = float1 / float2;
    }
    watch.Stop();
    txtTimeFloat.Text = "~" +
        (10 * watch.Elapsed.TotalSeconds).ToString() + " sec";
    txtTimeFloat.Refresh();

    watch.Reset();
    watch.Start();
    for (int i = 0; i < num_trials; i++)
    {
        int1 = 7331;
        int2 = 1337;
        int3 = int1 / int2;
    }
    watch.Stop();
    txtTimeInt.Text =
        watch.Elapsed.TotalSeconds.ToString() + " sec";
    txtTimeInt.Refresh();

    watch.Reset();
    watch.Start();
    for (int i = 0; i < num_trials; i++)
    {
        long1 = 73317331;
        long2 = 13371337;
        long3 = long1 / long2;
    }
    watch.Stop();
    txtTimeLong.Text =
        watch.Elapsed.TotalSeconds.ToString() + " sec";
    txtTimeLong.Refresh();

    watch.Reset();
    watch.Start();
    for (int i = 0; i < num_trials; i++)
    {
        byte1 = 231;
        byte2 = 123;
        byte3 = (byte)(byte1 / byte2);
    }
    watch.Stop();
    txtTimeByte.Text =
        watch.Elapsed.TotalSeconds.ToString() + " sec";
    txtTimeByte.Refresh();

    Cursor = Cursors.Default;
}

The most interesting part of the code performs a simple mathematical operation inside loops for each of the data types. The program also executes a similar loop for the float data type for comparison. Because the float loop is a lot slower than those that use the other data types, the code divides the number of iterations by 10 for float and then multiplies the elapsed time by 10.

Before running the program I thought that int would be faster than long, but I didn’t expect int to take only a third as long as long. I also didn’t expect the byte data type to have roughly the same performance as the int data type.

Note that operations involving byte always return the int data type so the code must cast the result back into the byte data type. Even with the extra cast operation, byte is about as fast as int.

The moral is, use the int type instead of long if you can. (Note that the results may be different on different computers.)


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in performance, variables and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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