user138283
user138283

Reputation:

Windows, Linux and Memory Management

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.

Upvotes: 2

Views: 1641

Answers (2)

avakar
avakar

Reputation: 32635

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);
}

Upvotes: 1

jmanning2k
jmanning2k

Reputation: 9478

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.

Upvotes: 6

Related Questions