在Windows上,试用版代码以32位运行的速度比Linux上64位的运行速度快两倍

编程入门 行业动态 更新时间:2024-10-28 08:19:06
本文介绍了在Windows上,试用版代码以32位运行的速度比Linux上64位的运行速度快两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一段代码在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位的运行速度快两倍

本文发布于:2023-10-18 11:42:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:试用版   速度快   两倍   速度   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!