Search code examples
c++sortinggnuintelstd-pair

Poor performance of GNU C++ compiler while sorting vector of pairs, compared with Intel


I have a vector of pairs in my program, which I need to sort. Something as:

std::vector<std::pair<int, uintmax_t> > temp;
...
std::sort(temp.begin(), temp.end());

I performed some measurements and found out that for vector of size something over 16M of elements, the sorting takes 3 seconds when compiled with Intel C++ compiler and 25 seconds when compiled with GNU C++ compiler. This seems to be an extreme difference to me (more than 8 times slower using GNU).

Do you know any way how to make this program faster with GNU C++?

My configuration is Intel 12.1.5 and GNU 4.7.1. Unfortunately, I have no superuser rights on the computer I use for program runs.

Thanks for help in advance, Daniel.

EDIT: The optimization flag -O3 solved this issue, GNU C++ now takes 3 to 4 seconds. Thanks for hint, shame on me I have not figured out it myself :(. So, I hope this post will help someone else one day:).

Just for information, I did not specify any optimization flags within my measurements (maybe -O2 is default both for Intel and GNU?).


Solution

  • Add flags -Ofast to turn on almost all possible optimization flags of g++.

    Yes, -O2/-O3 is enough for this problem. For more information about flags of g++ to optimize, see here https://stackoverflow.com/a/3005673/1095974.