How do you normally set up your compiler’s optimization settings?

Do you normally set your compiler to optimize for maximum speed or smallest code size? or do you manually configure individual optimization settings? Why?

I notice most of the time people tend to just leave compiler optimization settings to their default state, which with visual c++ means max speed. I’ve always felt that the default settings had more to do with looking good on benchmarks, which tend to be small programs that will fit entirely within the L2 cache than what’s best for overall performance, so I normally set it optimize for smallest size.


As a Gentoo user I have tried quite a few optimizations on the complete OS and there have been endless discussions on the Gentoo forums about it. Some good flags for GCC can be found in the wiki.

In short, optimizing for size worked best on an old Pentium3 laptop with limited ram, but on my main desktop machine with a Core2Duo, -O2 gave better results over all.

There’s also a small script if you are interested in the x86 (32 bit) specific flags that are the most optimized.

If you use gcc and really want to optimize a specific application, try ACOVEA. It runs a set of benchmarks, then recompile them with all possible combinations of compile flags. There’s an example using Huffman encoding on the site (lower is better):

A relative graph of fitnesses:

   Acovea Best-of-the-Best: **************************************                (2.55366)
     Acovea Common Options: *******************************************           (2.86788)
                       -O1: **********************************************        (3.0752)
                       -O2: ***********************************************       (3.12343)
                       -O3: ***********************************************       (3.1277)
           -O3 -ffast-math: **************************************************    (3.31539)
                       -Os: *************************************************     (3.30573)

(Note that it found -Os to be the slowest on this Opteron system.)

Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .