Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows vs. Linux memory allocation/std::list constructor performance

I'm porting C++ code from Linux to Windows. During this process, I found out that the following line takes ~10 times slower under Windows (on exactly the same hardware):

list<char*>* item = new list<char*>[160000]; 

On Windows it takes ~10ms, while on Linux it takes ~1ms. Note that this is the average time. Running this row 100 times takes ~1 second on Windows.

This happens both on win32 and x64, both versions are compiled in Release, and the speed is measured via QueryPerformanceCounter (Windows) and gettimeofday (Linux).

The Linux compiler is gcc. The Windows compiler is VS2010.

Any idea why could this happen?

like image 298
Doron Yaacoby Avatar asked Jan 26 '12 15:01

Doron Yaacoby


2 Answers

It could be more an issue of library implementation. I would expect a single allocation in most cases, with the default constructor for list not allocating anything. So what you're trying to measure is the cost of the default constructor of list (which is executed 160000).

I say "trying to measure", because any measurements that small are measuring clock jitter and resolution more than they're measuring code execution times. You should put this in a loop, to execute it frequently enough to get a runtime of a couple of seconds. And when you do this, you need to take precautions to ensure that the compiler doesn't optimize anything out.

And under Linux, you want to measure using clock(), at least; the wall clock time you get from gettimeofday is very dependent on what else happens to happen at the same time. (Don't use clock() under Windows, however. The Windows implementation is broken.)

like image 135
James Kanze Avatar answered Nov 13 '22 14:11

James Kanze


I think this instruction takes less time in both OS (regardless of anything). In this case It take such few time that you may be actually measuring the resolution of your timers.

like image 20
UmNyobe Avatar answered Nov 13 '22 14:11

UmNyobe