skip to content

What do GCC's -O0, -O1, -O2, -O3, -Os and -Og each trade off, and which level is the default?

level: juniorimportance: must knowfreq 72%

answer

  1. speed, size, debuggability, build time
  2. -O0 is the default
  3. -O2: no space-speed trade
  4. -O3 grows loops and code
  5. -Og for edit-compile-debug

basics

~20 s

-O0, the default, barely optimizes so builds are fast and debugging is exact. -O1 adds cheap optimizations, -O2 nearly all that do not trade size for speed, -O3 adds aggressive loop work, -Os favours size, and -Og keeps debugging usable.

solid answer

~50 s

With no `-O` option GCC uses **`-O0`**: it compiles quickly and keeps every statement independent, so a debugger shows exactly what the source says. `-O1` (or `-O`) enables optimizations that do not take much compile time. **`-O2`** performs nearly all supported optimizations that do not involve a space-speed tradeoff, and in GCC 16 that includes loop and SLP vectorization with the `very-cheap` cost model; it is the usual release level. **`-O3`** adds loop unswitching, peeling, distribution, predictive commoning and the `dynamic` vectorizer cost model, which can make code faster but larger, and is not guaranteed to win. **`-Os`** is `-O2` without the alignment and other code-growing options, tuned for size. **`-Og`** enables most of `-O1` minus passes that interfere with debugging. The last `-O` on the command line wins, and `-Q --help=optimizers` lists what a level really enables.

go deeper

for a junior

Say that -O0 is the default and good for debugging, -O2 is the usual release level, and -Os optimizes for size.

for a middle

Explain what separates -O2 from -O3, why -O0 ignores individual optimization flags, and why the last -O option wins.

for a senior

Choose levels per component from measurements, and recognise when -O3's code growth costs more than it saves.

for a principal

Own the build configurations' optimization defaults, and require a benchmark before any component departs from them.

## What an optimization level is An `-O` level is a bundle of individual `-f` flags plus pass settings. GCC's manual states the baseline: without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. Each higher level spends more compile time, and sometimes code size, to make the program faster. | Level | Goal | What it adds | Typical use | |---|---|---|---| | `-O0` | fast builds, exact debugging | almost nothing; **the default** | first debug builds | | `-O1` / `-O` | cheap wins | constant propagation, dead code elimination, `-fomit-frame-pointer` and others that are inexpensive to compute | large machine-generated code | | `-O2` | speed without size trade-offs | inlining of small and suitable functions, scheduling, `-fstrict-aliasing`, vectorization with the `very-cheap` cost model | release builds | | `-O3` | maximum speed attempts | `-funswitch-loops`, `-fpeel-loops`, `-ftree-loop-distribution`, `-fpredictive-commoning`, the `dynamic` vectorizer cost model | measured hot code | | `-Os` | small code | `-O2` minus alignment and other code-growing options, tuned for size | firmware, caches, downloads | | `-Og` | debuggable but optimized | `-O1` minus the passes that hurt debugging | edit-compile-debug loop | GCC 16 also has `-Oz`, which optimizes for size more aggressively than `-Os`, even if more instructions run. ## -O0 really means off The manual is explicit: at `-O0` most optimization passes are completely disabled and **do not run even if you enable them on the command line**. Adding `-finline-functions` to an `-O0` build changes nothing, because the analyses those passes need are only enabled from `-O1`. If you want one optimization, start from a level and add or remove flags on top of it. ## -O2 versus -O3 `-O2` is the level most projects ship. Its definition, nearly all optimizations without a space-speed tradeoff, keeps code size stable. In GCC 16 the `-O2` list includes `-ftree-loop-vectorize` and `-ftree-slp-vectorize` with `-fvect-cost-model=very-cheap`, so simple loops are vectorized only when the vectorizer is sure it pays. `-O3` removes that restraint. Loop transformations duplicate code, the `dynamic` cost model vectorizes more loops behind runtime checks, and cloning specializes functions for constant arguments. The result is often faster for numeric kernels and sometimes **slower** for branchy code, because bigger code puts more pressure on the instruction cache. The professional answer is to measure: build the hot component at both levels and benchmark it. ## -Os and -Og - `-Os` keeps the `-O2` optimizations except those that often increase size, such as function and loop alignment, and makes size the tuning goal. It is common for embedded firmware and for code where instruction-cache misses dominate. - `-Og` is what the manual recommends for the standard **edit-compile-debug** cycle. It enables the `-O1` flags except those known to interfere with debugging, and it turns on variable tracking, so a debugger shows more variables correctly than at `-O0` in function prologues and epilogues while the code still runs much faster than `-O0`. ## Rules that surprise people 1. If several `-O` options appear, **the last one wins**. A build system that appends `-O2` after your `-O3` silently gives you `-O2`. 2. A different result at `-O2` than at `-O0` almost always means the program has **undefined behaviour** or depends on unspecified details, not that the optimizer is wrong. Floating point is the honest exception: GCC's default `-ffp-contract=fast` for C++ lets optimized code fuse a multiply and an add on targets with FMA, which can change the last bits, and `-Ofast` relaxes the rules much further. 3. The exact flag set depends on the target and on how GCC was configured. `g++ -Q -O2 --help=optimizers` prints what is enabled for your compiler. 4. Higher levels make some warnings appear, because the analyses behind them only run when optimizing. ## Choosing in practice - Debug configuration: `-Og -g` or `-O0 -g`. - Release configuration: `-O2 -g`, with debug information split off. - Hot numeric code: try `-O3`, and keep it only where measurements show a gain. - Size-bound targets: `-Os`, or `-Oz` when every byte counts.

  • Why might a program be slower at -O3 than at -O2?
    `-O3` duplicates code through loop unswitching, peeling and versioning, and vectorizes more loops behind runtime checks. If those paths are cold or the loops are short, the extra code costs instruction-cache space and branch overhead without paying back. Measure per component instead of assuming higher is faster.
  • How do you see exactly which optimizations a level enables in your GCC?
    Run `g++ -Q -O2 --help=optimizers` and compare it with another level. The set depends on the target and configuration, so this beats memorizing lists, and it shows the effect of adding or removing individual `-f` flags.

  • GCC optimizes at -O2 unless told otherwise.
  • Adding -f flags to an -O0 build enables those optimizations.
  • -O3 is always faster than -O2.
  • If a program prints different results at -O0 and -O2, the optimizer must have a bug.
  • -Og means no optimization with extra debug information.

Assumes GCC 16, whose -O2 includes loop and SLP vectorization with the very-cheap cost model and which documents -Oz.