Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling in GCC: Is -O3 harmful?

I have heard that one should not compile with -O3 option with gcc. Is that true? If so, what are the reasons for avoiding -O3?

like image 551
user877329 Avatar asked Aug 08 '14 07:08

user877329


1 Answers

The answer is: it depends on your code.

The basic rule of thumb is like this:

  • At -O1 the compiler does optimizations that don't take too long to compute.

  • At -O2 the compiler does "expensive" optimizations that may slow the compile process. They might also make the output program a little larger, but probably not so much.

  • -Os is roughly the same as -O2, but the optimizations are tuned more towards size than speed. For the most part these two features don't conflict (more optimal code does less steps and is therefore smaller), but there are some tricks that duplicate code to avoid branching penalties, for example.

  • At -O3 the compiler really cranks up the space-hungry optimizations. It will inline functions much more aggressively, and try to use vectorization where possible.

You can read more details in the GCC documentation. If you really want to super optimize your code then you can try to enable even more options not used even at -O3; the -floop-* options, for instance`.

The problem with speed-space optimizations, in particular, is that they can have a negative impact on the effectiveness of your memory caches. The code might be better for the CPU, but if it's not better for your memory, then you lose. For this reason, if your program doesn't have a single hot-spot where it spends all it's time then you might find it is slowed down overall.

Real-world optimization is a imprecise science for three reasons:

  1. User's hardware varies a lot.

  2. What's good for one code base might not be good for another.

  3. We want the compiler to run quickly, so it must make best guesses, rather than trying all the options and picking the best.

Basically, the answer is always, if performance matters try all the optimization levels, measure how well your code performs, and choose the best one for you. And do this again every something big changes.

If performance does not matter, -O2 is the choice for you.

like image 125
ams Avatar answered Sep 28 '22 00:09

ams