2

Should I always specify the -O3 flag when compiling a release version with gcc, or is there any other possible drawback?

1 Answers1

0

Should I always specify the -O3 flag when compiling a release version with gcc?

No, or at least maybe not. For performance; sometimes -O3 makes code that is slower than you get from -O2.

Under the hood; it's really a bunch of different optimizations that can be enabled/disabled individually; where -O3 (and -O2 and -Os) is just a convenient shorthand for enabling a group of many optimizations. -O2 is supposed to represent "enable all optimizations that always help", and -O3 is supposed to represent "enable all optimizations that often help (but may make things worse)". Which actual optimizations are/aren't enabled for each -O setting is detailed in the manual (at https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html ).

If you don't use the shortcuts and specify individual optimizations yourself; then (using a laborious "trial and error" approach and benchmarking the result for each case) you can find the set of optimizations that always help your program (and avoid enabling optimizations that make the performance of your program worse).

A more practical approach would be to start with O2 then determine which of the optimizations that aren't already enabled by -O2 also help.

However; performance isn't the only thing that matters. To save time; most people just try -O2 or -O3 and pick whatever seems fastest. Part of the reason for this is that your software and the compiler is constantly changing; so any "laborious benchmarking" you do would need to be done again regularly.

Note: to actually get the maximum performance possible, each translation unit can be compiled with different optimization settings (so you can do the "laborious trial and error" for each individual source file); and then the resulting set of "optimized differently" object files can be fed into a link-time optimizer to optimize more.

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • "a bunch of different optimizations that can be enabled/disabled individually": not really, some parts of the compiler check directly `optimize >= 3`. – Marc Glisse Feb 28 '21 at 09:35