试除代码在 Windows 上的运行速度是 32 位的 2 倍,比在 Linux 上的 64 位快 [英] Trial-division code runs 2x faster as 32-bit on Windows than 64-bit on Linux
问题描述
我有一段代码在 Windows 上的运行速度比在 linux 上快 2 倍.这是我测量的时间:
I have a piece of code that runs 2x faster on windows than on linux. Here are the times I measured:
g++ -Ofast -march=native -m64
29.1123
g++ -Ofast -march=native
29.0497
clang++ -Ofast -march=native
28.9192
visual studio 2013 Debug 32b
13.8802
visual studio 2013 Release 32b
12.5569
看起来差别真的太大了.
It really seems to be too huge a difference.
代码如下:
#include <iostream>
#include <map>
#include <chrono>
static std::size_t Count = 1000;
static std::size_t MaxNum = 50000000;
bool IsPrime(std::size_t num)
{
for (std::size_t i = 2; i < num; i++)
{
if (num % i == 0)
return false;
}
return true;
}
int main()
{
auto start = std::chrono::steady_clock::now();
std::map<std::size_t, bool> value;
for (std::size_t i = 0; i < Count; i++)
{
value[i] = IsPrime(i);
value[MaxNum - i] = IsPrime(MaxNum - i);
}
std::chrono::duration<double> serialTime = std::chrono::steady_clock::now() - start;
std::cout << "Serial time = " << serialTime.count() << std::endl;
system("pause");
return 0;
}
所有这些都是在同一台机器上测量的,windows 8 vs linux 3.19.5(gcc 4.9.2,clang 3.5.0).linux和windows都是64位的.
All of this was measured on the same machine with windows 8 vs linux 3.19.5(gcc 4.9.2, clang 3.5.0). Both linux and windows are 64bit.
这可能是什么原因?一些调度程序问题?
What could be the reason for this? Some scheduler issues?
推荐答案
你不要说 windows/linux 操作系统是 32 位还是 64 位.
You don't say whether the windows/linux operating systems are 32 or 64 bit.
在 64 位 linux 机器上,如果您将 size_t 更改为 int,您会发现在 linux 上的执行时间下降到与您在 windows 上的执行时间相似.
On a 64-bit linux machine, if you change the size_t to an int you'll find that execution times drop on linux to a similar value to those that you have for windows.
size_t 在 win32 上是 int32,在 win64 上是 int64.
size_t is an int32 on win32, an int64 on win64.
刚刚看到你的窗户拆卸.
just seen your windows disassembly.
您的 Windows 操作系统是 32 位版本(或者至少您已编译为 32 位).
Your windows OS is the 32-bit variety (or at least you've compiled for 32-bit).
这篇关于试除代码在 Windows 上的运行速度是 32 位的 2 倍,比在 Linux 上的 64 位快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!