views:

812

answers:

4

How many GCC optimization levels are there?

I tried gcc -O1, gcc -O2, gcc -O3, and gcc -O4

If I use a really large number, it won't work.

However, I have tried

gcc -O100

and it compiled.

How many optimization levels are there?

+1  A: 

There are four: -O0 (no optimization) up to -O3 (maximum). Everything else is the same as -O3.

Konrad Rudolph
+4  A: 

Four (0-3): See the GCC 4.4.2 manual. Anything higher is just -O3, but at some point you will overflow the variable size limit.

Tom
+7  A: 

Five: -O0...-O3, and -Os.

Anything higher than O3 is the same as O3, and note that O3 may not always produce faster code than O2.

drhirsch
-Os is a variant on -O2, enabling all -O2 options except those what usually cause code size to increase. The following are disabled: -falign-functions -falign-jumps -falign-loops -falign-labels -freorder-blocks -freorder-blocks-and-partition -fprefetch-loop-arrays -ftree-vect-loop-version
Tom
And it is well known that sometimes -O2 decreases the speed of real world programs compared to -Os because of cache issues, although the same algorithm may run faster with -O2 or -O3 in a benchmark setting.
drhirsch
+14  A: 

To be pedantic, there are 6 different valid -O options you can give to gcc, though there are some that mean the same thing. From the man page

-O (Same as -O1)

-O0 (do no optimisation, the default if no optimisation level is specified)

-O1 (optimise)

-O2 (optimise even more)

-O3 (optimise the most)

-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. -Os disables the following optimization flags: -falign-functions -falign-jumps -falign-loops -falign-labels -freorder-blocks -freorder-blocks-and-partition -fprefetch-loop-arrays -ftree-vect-loop-version)

Glen
There's a typo in -O3, it's a zero instead of an O.
Johan Dahlin
If you're developing on Mac OS X there's an additional `-Oz` setting which is "optimize for size more aggressively than `-Os`": http://developer.apple.com/mac/library/DOCUMENTATION/DeveloperTools/gcc-4.0.1/gcc/Optimize-Options.html
pauldoo