0

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?).

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
  • 1
    Could you please post the entire test program so that we can try to replicate your results and then analyze them. The first step to making it faster is figuring out why it takes so long. (I'd use `callgrind` personally.) – David Schwartz Sep 27 '12 at 17:29
  • @Anyconr: thanks, it helped, see edited question for details :) – Daniel Langr Sep 27 '12 at 17:42
  • @David Schwartz: I have no test program, I just put some log messages into code. It is little bit long to be posted here. – Daniel Langr Sep 27 '12 at 17:44
  • @DanielLangr: You probably didn't want to use `-O3` unless you have to. `-O2` is common for release builds or performance-testing builds. – David Schwartz Sep 27 '12 at 17:45
  • @DanielLangr: The default for gcc is `-O0` -- disable ALL optimiztions. Most compilers default to 'compile as fast as possible, with minimal/no optimization'. Gcc sometimes actually runs FASTER with `-O1` as the resulting object files are so much smaller and link faster. But if you care about performance of the generated program, you want at least `-O2` – Chris Dodd Sep 27 '12 at 17:49
  • @Chris Dodd: Good to know, don't know why I thought -O2 is default. Will remember this. – Daniel Langr Sep 27 '12 at 17:50

1 Answers1

1

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.

Community
  • 1
  • 1
chyx
  • 501
  • 2
  • 10