[英]Performance difference between AnyCPU vs x64 platform on a 64 bit machine
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. 根据此质量检查 ,在64位计算机上运行时,使用Any CPU构建的应用程序和x64在性能上应该没有任何区别,但是当我专门为x64平台构建时,我的用例性能会出现双倍增长。
My use case is for manipulating 64 bit bitboards , the majority of processing is on bit operations and arithmetic on ulong
variables. 我的用例是操纵64位位板 ,大多数处理是对位运算和对
ulong
变量的算术运算。
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
在使用任何CPU平台的Release build中,结果为:
Sum = 9745675244420464640, took 13148ms
In Release build with x64 platform the results are: Sum = 9745675244420464640, took 5693ms
在使用x64平台的Release build中,结果为:
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?
如果通常的假设是,任何CPU vs x64版本都应在64位计算机上执行相同的工作,为什么会有如此大的差异?
No, you compared perf between 32-bit and 64-bit code. 不,您比较了32位和64位代码之间的性能。 The x64 flavor is much faster because it can do the math with a single 64-bit processor register.
x64版本的速度更快,因为它可以使用单个64位处理器寄存器进行数学运算。 It is much more of a slog in 32-bit machine code, it has to juggle two 32-bit registers.
它更像是32位机器代码中的一个口号,它必须处理两个32位寄存器。 Easy to see the difference between the two with Debug > Windows > Disassembly.
通过“调试”>“ Windows”>“反汇编”,可以轻松看出两者之间的区别。
This went wrong because you paid too much attention to the solution platform name. 这出了错,因为您过多地关注了解决方案平台名称。 Too prominently displayed, but a selection that only matters to C++ projects.
突出显示,但是选择仅对C ++项目重要。 And, unfortunately, on UWP projects due to .NET Native.
而且,不幸的是,由于.NET Native,在UWP项目上。 It selects the build tool flavors, C# has only one compiler that can target any platform.
它选择了构建工具类型,C#仅具有一个可以针对任何平台的编译器。
The real settings that matters are the jitter forcing options. 重要的实际设置是抖动强制选项。 Project > Properties > Build tab.
项目>属性>构建选项卡。 Select the Release build if necessary and choose the Platform target and "Prefer 32-bit" settings.
如有必要,选择发布版本,然后选择平台目标和“首选32位”设置。 Untick the latter.
取消勾选后者。 You'll now see that AnyCPU == x64 on a 64-bit operating system.
现在,您将看到在64位操作系统上,AnyCPU == x64。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.