Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Int32 vs Int64 performance on a 64 bit platform?

Tags:

.net

My question is relatively simply. On 32bit platforms it's best to use Int32 as opposed to short or long due to to the cpu processing 32 bits at a time. So on a 64 bit architecture does this mean it's faster to use longs for performance? I created a quick and dirty app that copies int and long arrays to test the benchmarks. Here is the code(I did warn it's dirty):

static void Main(string[] args)
    {
        var lar = new long[256];

        for(int z = 1; z<=256;z++)
        {
            lar[z-1] = z;
        }
        var watch = DateTime.Now;
        for (int z = 0; z < 100000000; z++)
        {
            var lard = new long[256];
            lar.CopyTo(lard, 0);

        }
        var res2 = watch - DateTime.Now;

        var iar = new int[256];

        for (int z = 1; z <= 256; z++)
        {
            iar[z - 1] = z;
        }

        watch = DateTime.Now;

        for (int z = 0; z < 100000000; z++)
        {
            var iard = new int[256];
            iar.CopyTo(iar, 0);

        }

        var res1 = watch - DateTime.Now;
        Console.WriteLine(res1);
        Console.WriteLine(res2);


    }

The results it produces make long about 3 times as fast as int. Which makes me curious to whether I should start using longs for counters and such. I also did a similar counter test and long was insignificantly faster. Does anybody have any input on this? I also understand even if longs are faster they will still take up twice as much space.

like image 325
Omego2K Avatar asked Sep 11 '11 02:09

Omego2K


People also ask

Is Int32 faster than Int64?

Int32 will be equally as efficient on all x86, x64, and IA64. On an x64 and on an IA64 both Int32 and Int64 are equally as efficient. On an x86 Int32 will be more efficient than an Int64.

What is difference between Int32 and Int64?

Int32 is used to represents 32-bit signed integers . Int64 is used to represents 64-bit signed integers.

Is Int64 same as long?

In C#, Int64 Struct is used to represent 64-bit signed integer(also termed as long data type) starting from range -9,223,372,036,854,775,808 to +9, 223,372,036,854,775,807.

What does Int64 mean?

Remarks. Int64 is an immutable value type that represents signed integers with values that range from negative 9,223,372,036,854,775,808 (which is represented by the Int64. MinValue constant) through positive 9,223,372,036,854,775,807 (which is represented by the Int64.


1 Answers

No. It takes longer to do 64-bit on a 32-bit CPU because the CPU can only handle 32-bits at a time. A 64-bit CPU will just ignore the missing 32-bits.

Also, try not to preemptively optimize too much. Only optimize when there is a noticeable bottleneck.

like image 130
beatgammit Avatar answered Sep 28 '22 05:09

beatgammit