Linux 上的相同程序比 Windows 更快——为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8071851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Same program faster on Linux than Windows -- why?
提问by Szabolcs
The solutionto this was found in the question Executable runs faster on Wine than Windows -- why?Glibc's floor()
is probably implemented in terms of system libraries.
这个问题的解决方案是在Executable 在 Wine 上运行速度比 Windows 快的问题中找到的——为什么?Glibc 的floor()
可能是根据系统库实现的。
I have a very small C++ program (~100 lines) for a physics simulation. I have compiled it with gcc 4.6.1 on both Ubuntu Oneiric and Windows XP on the same computer. I used precisely the same command line options (same makefile).
我有一个非常小的 C++ 程序(约 100 行)用于物理模拟。我在同一台计算机上的 Ubuntu Oneiric 和 Windows XP 上使用 gcc 4.6.1 编译了它。我使用了完全相同的命令行选项(相同的 makefile)。
Strangely, on Ubuntu, the program finishes muchfaster than on Windows (~7.5 s vs 13.5 s). At this point I thought it's a compiler difference (despite using the same version).
奇怪的是,在 Ubuntu 上,程序的运行速度比在 Windows 上快得多(~7.5 s vs 13.5 s)。此时我认为这是编译器的差异(尽管使用相同的版本)。
But even more strangely, if I run the Windows executable under wine, it's still faster than on Windows (I get 11 s "real" and 7.7 s "user" time -- and this includes wine startup.)
但更奇怪的是,如果我在 wine 下运行 Windows 可执行文件,它仍然比在 Windows 上快(我得到 11 秒的“真实”时间和 7.7 秒的“用户”时间——这包括 wine 启动。)
I'm confused. Surely if the same code is run on the same CPU, there shouldn't be a difference in the timing.
我糊涂了。当然,如果相同的代码在同一个 CPU 上运行,那么时间上应该没有区别。
What can be the reason for this?What could I be doing wrong?
这是什么原因?我可能做错了什么?
The program does minimal I/O (outputs a single line), and only uses a fixed-length vector
from the STL (i.e. no system libraries should be involved). On Ubuntu I used the default gcc and on Windows the Nuwen distribution. I verified that the CPU usage is close to zero when doing the benchmarking (I closed most programs). On Linux I used time
for timing. On Windows I used timethis.exe
.
该程序执行最少的 I/O(输出单行),并且仅使用vector
来自 STL的固定长度(即不应涉及系统库)。在 Ubuntu 上我使用默认的 gcc,在 Windows 上使用Nuwen 发行版。我在进行基准测试时验证了 CPU 使用率接近于零(我关闭了大多数程序)。在 Linux 上我用于time
计时。在 Windows 上,我使用了timethis.exe
.
UPDATE
更新
I did some more precise timings, comparing the running time for different inputs (run-time must be proportional to the input) of the gcc and msvc-compiled programs on Windows XP, Wine and Linux. All numbers are in seconds and are the minimum of at least 3 runs.
我做了一些更精确的计时,比较了 Windows XP、Wine 和 Linux 上 gcc 和 msvc 编译程序的不同输入(运行时间必须与输入成正比)的运行时间。所有数字都以秒为单位,并且是至少 3 次运行中的最小值。
On Windows I used timethis.exe(wall time), on Linux and Wine I used time (CPU time). (timethis.exe is broken on Wine) I made sure no other programs were using the CPU and disabled the virus scanner.
在 Windows 上我使用timethis.exe(挂机时间),在 Linux 和 Wine 上我使用时间(CPU 时间)。(timethis.exe 在 Wine 上被破坏)我确保没有其他程序正在使用 CPU 并禁用病毒扫描程序。
The command line options to gcc were -march=pentium-m -Wall -O3 -fno-exceptions -fno-rtti
(i.e. exceptions were disabled).
gcc 的命令行选项是-march=pentium-m -Wall -O3 -fno-exceptions -fno-rtti
(即异常被禁用)。
What we see from this data:
我们从这些数据中看到:
the difference is not due to process startup time, as run-times are proportional to the input
The difference between running on Wine and Windows exists only for the gcc-compiled program, not the msvc-compiled one: it can't be casued by other programs hogging the CPU on Windows or timethis.exe being broken.
差异不是由于进程启动时间,因为运行时间与输入成正比
在 Wine 和 Windows 上运行的区别只存在于 gcc 编译的程序,而不是 msvc 编译的程序:它不能被其他程序占用 Windows 上的 CPU 或 timethis.exe 被破坏。
回答by rubenvb
You'd be surprised what system libraries are involved. Just do ldd on your app, and see which are used (ok, not that much, but certainly glibc).
您会对所涉及的系统库感到惊讶。只需在您的应用程序上执行 ldd,看看使用了哪些(好吧,不是那么多,但肯定是 glibc)。
In order to completely trust your findings about execution speed, you would need to run your app a couple of times sequentially and take the mean execution time. It might be that the OS loader is just slower (although 4s is a long loading time).
为了完全相信你对执行速度的发现,你需要按顺序运行你的应用程序几次,并取平均执行时间。可能是 OS 加载程序速度较慢(尽管 4s 是很长的加载时间)。
Other very possible reasons are:
其他非常可能的原因是:
- Different malloc implementation
- Exception handling, if used to the extreme might cause slowdown (Windows GCC, MinGW, might not be the optimal exception handling star of the show)
- OS-dependent initialization: stuff that needs to be done at program startup on Windows, but not on Linux.
- 不同的 malloc 实现
- 异常处理,如果使用到极致可能会导致速度变慢(Windows GCC,MinGW,可能不是节目中最佳的异常处理之星)
- 依赖于操作系统的初始化:需要在 Windows 上的程序启动时完成的事情,而不是在 Linux 上。
Most of these are easily benchmarkable ;-)
其中大多数很容易进行基准测试;-)
An update to your update: the only thing you can now do is profile. Stop guessing, and let a profiler tell you where time is being spent. Use gprof and the Visual Studio built-in profiler and compare time spent in different functions.
更新您的更新:您现在唯一能做的就是个人资料。停止猜测,让分析器告诉您时间都花在了什么地方。使用 gprof 和 Visual Studio 内置分析器并比较在不同函数中花费的时间。
回答by neagoegab
Do benchmarking in code. Also try to compile with visual studio. On windows if you have some application like Yahoo Messenger, that are installing hooks, they can very easy slow down your application loading times.
在代码中进行基准测试。也尝试用visual studio编译。在 Windows 上,如果您有一些应用程序,例如 Yahoo Messenger,安装了钩子,它们可以很容易地减慢您的应用程序加载时间。
On windows you have: QueryPerformanceCounter On Linux: clock_gettime
在 Windows 上,您有:QueryPerformanceCounter 在 Linux 上:clock_gettime
回答by Basile Starynkevitch
Apparently the difference is system related.
显然,差异与系统有关。
You might use strace
to understand what system calls are done, eg
您可能会strace
用来了解完成了哪些系统调用,例如
strace -o /tmp/yourprog.tr yourprog
and then look into /tmp/yourprog.tr
然后调查 /tmp/yourprog.tr
(If an equivalent of strace
existed on Windows, try to use it)
(如果strace
Windows上存在等效项,请尝试使用它)
Perhaps your program is allocating memory (using mmap
system call), and perhaps the memory related system calls are faster on Linux (or even on Wine) than on Windows? Or some other syscalls give faster functionality on Linux that on Windows.
也许您的程序正在分配内存(使用mmap
系统调用),也许与内存相关的系统调用在 Linux(甚至在 Wine)上比在 Windows 上更快?或者其他一些系统调用在 Linux 上提供比在 Windows 上更快的功能。
NB. I know nothing about Windows, since I'm using Unix systems since 1986 and Linux since 1993.
注意。我对 Windows 一无所知,因为我从 1986 年开始使用 Unix 系统,从 1993 年开始使用 Linux。