tags:

views:

335

answers:

2

I'm pretty curious about how Windows and Linux does memory management with C++ programs.

The reason of this curiosity is because I've just made 3 very simple programs in C++ portable between Linux and Windows. The code was exactly the same. The hardware too. But the results were incredibly different! Both tests were repeated 10 times and then the arithmetic mean was calculated.

I've tested sequential insertions on a static array of integers, on the class vector and at a stack (with pointers). The total number of insertions was 10^6.

Windows XP SP2 x86 results: Static array of integers: 56 ms Class vector: 686 ms Stack (with pointers): 2193 ms

Slackware 11 x86 results: Static array of integers: 100 ms Class vector: 476 ms Stack (with pointer): 505 ms

The speed difference between the stack insertion time on Windows and Slax is impressive. Does these results seem normal? Both codes were compiled using G++ (mingw32-g++ on Windows).

The computer used was a Dual Core 3.2Ghz with 4GB RAM and when the tests were made, there were more than 2GB of free RAM.

+6  A: 

This may have more to do with the C++ stdlib implementation (Microsoft MSVC vs. GNU libc/stdc++) than memory management...

C++ sets the spec, but implementations vary greatly.

Update: And as I now notice that you used g++ on both platforms - well, it's still a different implementation. And the GNU code was developed for a Unix environment, and was ported to Windows later - so it may make assumptions and optimizations that are incorrect for a Windows environment.

But your original question is valid. It may have something to do with the underlying memory model of the operating system - but your tests here are too coarse to draw any conclusions.

If you run more tests, or try using different flags / compilers, please post some updated stats, they would be informative.

jmanning2k
+1 vote here. And to Farofeiro, you should try to compile on windows using MS compiler, not gcc. It might (but just <i>might</i>) make it better.
Nir Levy
Ok, I can try this. But why you guess it should be better? G++ for windows isn't well optimized for it or VS makes code much more optimized for windows?
Farofeiro
+2  A: 

Two seconds for a million insertions is a little too high. Have you enabled optimizations? Without them, those numbers mean nothing.

Edit: Since you compiled without optimizations, enable them (use -O2 for example) and measure again. The difference will probably be much smaller. The standard libraries tend to be quite defensive and perform a lot of consistency checking, which can skew the measurement a lot.

Edit: If it still takes over 2 seconds even with optimizations enabled, there something else going on. Try posting some code. The following program runs about 40 milliseconds on my system under cygwin.

#include <stack>

int main()
{
    std::stack<int> s;
    for (int i = 0; i < 1000000; ++i)
        s.push(i);
}
avakar
No, I haven't enabled any kind of optimizations. But the way I compiled the windows version was exactly the same way I compiled the Linux one.
Farofeiro
Ok, I've just recompiled the windows version and now the stack does the 10^6 insertions in 2121 ms. There's a reduction, but still the difference is very high. I have compiled with only -O2 flag
Farofeiro