同样的程序在Linux上比Windows快 - 为什么? [英] Same program faster on Linux than Windows -- why?

查看:760
本文介绍了同样的程序在Linux上比Windows快 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案可在可执行文件在Windows上运行速度比Windows快 - 为什么? Glibc的 floor()可能是在系统库方面实现的。






我有一个非常小的C ++程序(〜100行)用于物理模拟。我已经编译它与gcc 4.6.1在同一台计算机上的Ubuntu Oneiric和Windows XP。我使用了完全相同的命令行选项(相同的makefile)。



奇怪的是,在Ubuntu上,程序比 7.5秒对13.5秒)。在这一点上,我认为这是一个编译器差异(尽管使用相同的版本)。



但更奇怪的是,如果我运行Windows可执行文件下,在Windows上(我得到11秒的真实和7.7秒的用户时间 - 这包括葡萄酒的启动。)



我很困惑。当然,如果相同的代码在同一个CPU上运行,时间上不应该有差别。



可能是什么原因? / strong>我可以做什么错误?



程序执行最少的I / O(输出一行),并且只使用固定长度向量(即不涉及系统库)。在Ubuntu上,我使用默认gcc,在Windows上使用 Nuwen发行版。我验证了在做基准测试时(我关闭大多数程序)的CPU使用率接近于零。在Linux上,我使用 time 定时。在Windows上,我使用了 timethis.exe



UPDATE
$ b

我做了一些更精确的计时,比较了Windows XP,Wine和Linux上的gcc和msvc编译程序的不同输入的运行时间(运行时间必须与输入成正比)。



在Windows上,我使用了 timethis.exe (wall time),在Linux和Wine上我使用时间(CPU时间)。 (timethis.exe在Wine上断开)我确保没有其他程序正在使用CPU并禁用病毒扫描程序。



gcc的命令行选项为 -march = pentium-m -Wall -O3 -fno-exceptions -fno-rtti (例外被禁用)。





我们从这里看到数据:


  1. 这不同于过程启动时间,因为运行时间与输入成正比


  2. 在Wine和Windows之间运行的区别仅在于gcc编译的程序,而不是msvc编译的程序:它不能由其他程序在Windows上占用CPU或timethis.exe损坏。



解决方案

你会惊讶什么系统库涉及。只要在你的应用程序上做ldd,看看使用哪些(确定,不是那么多,但肯定是glibc)。



为了完全信任你的发现,执行速度,您需要顺序运行您的应用程序几次,并采取平均执行时间。这可能是因为OS加载器只是较慢(虽然4s是一个长的加载时间)。



其他很可能的原因是:


  1. 不同的malloc实现

  2. 异常处理,如果使用极端可能会导致减速(Windows GCC,MinGW,



其中大部分都容易进行基准测试;






你的更新:你现在可以做的唯一的事情是配置文件。停止猜测,让分析器告诉你在哪里花费时间。使用gprof和Visual Studio内置分析器,并比较花在不同函数中的时间。


The solution to 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.


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).

Strangely, on Ubuntu, the program finishes much faster 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).

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.)

I'm confused. Surely if the same code is run on the same CPU, there shouldn't be a difference in the timing.

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.

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.

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.

The command line options to gcc were -march=pentium-m -Wall -O3 -fno-exceptions -fno-rtti (i.e. exceptions were disabled).

What we see from this data:

  1. the difference is not due to process startup time, as run-times are proportional to the input

  2. 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.

解决方案

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).

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).

Other very possible reasons are:

  1. Different malloc implementation
  2. Exception handling, if used to the extreme might cause slowdown (Windows GCC, MinGW, might not be the optimal exception handling star of the show)
  3. OS-dependent initialization: stuff that needs to be done at program startup on Windows, but not on 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.

这篇关于同样的程序在Linux上比Windows快 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆