Search code examples
gcccompiler-optimizationcompiler-flags

How much should I optimize?


In regards to optimizations done by the compiler (GCC), what is the standard practice? What does each option (-O, -O1, -O2, -O3, -Os, -s, -fexpensive-optimizations) do differently, and how do I decide what is optimal?


Solution

  • Usually -O2 is a good optimization level to try first.

    However, if you want the best result possible, you will end up trying many optimization levels as you can't tell beforehand which level will be best for your app.

    Also take note that optimization results should vary with each CPU (on some CPUs optimizing for size might actually yield a better speed than optimizing for speed).

    Just for future reference, here's a brief description of each level (you can find the complete description in the documentation http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html):

    -O (identical to -O1): With -O, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time.

    -O2: Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O, this option increases both compilation time and the performance of the generated code.

    -O3: Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize, -ftree-partial-pre and -fipa-cp-clone options.

    -Os: Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.

    -Ofast: Disregard strict standards compliance. -Ofast enables all -O3 optimizations. It also enables optimizations that are not valid for all standard compliant programs. It turns on -ffast-math and the Fortran-specific -fno-protect-parens and -fstack-arrays. If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.