Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why list::push_back much slower in VC++ than g++?

This code takes about 20 seconds in my VS2012, but only 1.x seconds in G++. Both in win8 x64 and compiled with default options.

list<double> items;
for(int i=0;i<10000000;i++){
    items.push_back(rand());
}
cout<<"done"<<endl;

Is it something about memory allocation? It takes 3~5 seconds to release memory after the ouput in VC++ in my machine, and even more than 1 minute in my firend's (win7 x64).

like image 684
raulchen Avatar asked Nov 27 '12 15:11

raulchen


1 Answers

Hmm...I expanded your code to include timing:

#include <list>
#include <iostream>
#include <time.h>
#include <stdlib.h>

int main() { 
    std::list<double> items;

    clock_t start = clock();

    for(int i=0;i<10000000;i++){
        items.push_back(rand());
    }

    clock_t finish = clock();

    std::cout << "Time: " << double(finish-start)/CLOCKS_PER_SEC << "\n";
    return 0;
}

I compiled with VC++ using: cl /O2b2 /GL test_list.cpp

Likewise, I compiled with g++, using: g++ -O3 test_list.cpp

Then I ran the two.

With VC++ I got: Time: 1.293.
With g++ I got: Time: 1.313.

That's a small enough difference that I think I'd need to test quite a bit more to be at all certain of saying VC++ produced significantly faster code, but I think it's enough to support a conclusion that VC++ is not producing significantly slower code.

You need to turn on optimization for timing results to mean anything.

like image 156
Jerry Coffin Avatar answered Oct 04 '22 18:10

Jerry Coffin