According to this QA there should be no performance difference between application built using Any CPU and x64 when run on a 64 bit machine, however I'm seeing around a double increase in performance in my use case when I specifically built for the x64 platform.
My use case is for manipulating 64 bit bitboards, the majority of processing is on bit operations and arithmetic on ulong
variables.
As an example:
public static ulong ReverseBits(ulong x)
{
ulong t;
x = (x << 32) | (x >> 32); // Swap register halves.
x = (x & 0x0001FFFF0001FFFFUL) << 15 | // Rotate left
(x & 0xFFFE0000FFFE0000UL) >> 17; // 15.
t = (x ^ (x >> 10)) & 0x003F801F003F801FUL;
x = (t | (t << 10)) ^ x;
t = (x ^ (x >> 4)) & 0x0E0384210E038421UL;
x = (t | (t << 4)) ^ x;
t = (x ^ (x >> 2)) & 0x2248884222488842UL;
x = (t | (t << 2)) ^ x;
return x;
}
static void Main(string[] args)
{
ulong sum = 0;
var s = Stopwatch.StartNew();
for (ulong i = 0; i < 1000000000; i++)
{
sum += ReverseBits(i);
}
s.Stop();
Console.WriteLine("Sum = {0}, took {1}ms", sum, s.ElapsedMilliseconds);
Console.ReadLine();
}
In Release build with Any CPU platform the results are:
Sum = 9745675244420464640, took 13148ms
In Release build with x64 platform the results are: Sum = 9745675244420464640, took 5693ms
That's more than double performance increase. Why is there such a big difference if the common assumption is that Any CPU vs x64 builds should perform the same on a 64 bit machine?