我有一段代码在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看来差异实在太大了。
这是代码:
#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和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的执行时间下降到与您拥有的执行时间相似的值
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.
编辑:刚刚看到了Windows反汇编。
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位运行的速度比Linux上64位的运行速度快两倍
发布评论