Igor
Igor

Reputation: 27250

When to use -O2 flag for gcc?

If I use "-O2" flag, the performance improves, but the compilation time gets longer.

How can I decide, whether to use it or not?

Maybe O2 makes the most difference in some certain types of code (e.g. math calculations?), and I should use it only for those parts of the project?

EDIT: I want to emphasize the fact that setting -O2 for all components of my project changes the total compilation time from 10 minutes to 30 minutes.

Upvotes: 13

Views: 14843

Answers (6)

jakobengblom2
jakobengblom2

Reputation: 5888

Is the increased compilation time really noticable? I use -O2 all the time as the default, anything less just leaves a lot of "friction" in your code. Also note that the optimization levels of -O1, -O2 tends to be the best tested, as they are most interesting. -O0 tends to be more buggy, and you can debug pretty well at -O2 in my experience. Provided you have some idea about what a compiler can do in terms of code reordering, inlining, etc.

-Werror -Wall is necessary.

Upvotes: 2

lothar
lothar

Reputation: 20209

We usually have our build environment set up so that we can build debug builds that use -O0 and release builds that use -O3 (the build enviroment preserves the objects and libraries of all configurations, so that one can switch easily between configurations). During development one mostly builds and runs the debug configuration for faster build speed (and more accurate debug information) and less frequently also builds and tests the release configuration.

Upvotes: 3

Christoffer
Christoffer

Reputation: 12910

Never.

Use -O3 -Wall -Werror -std=[whatever your code base should follow]

Upvotes: 6

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

I'm in bioinformatics so my advice may be biased. That said, I always use the -O3 switch (for release and test builds, that is; not usually for debugging). True, it has certain disadvantages, namely increasing compile-time and often the size of the executable.

However, the first factor can be partially mitigated by a good build strategy and other tricks reducing the overall build time. Also, since most of the compilation is really I/O bound, the increase of compile time is often not that pronounced.

The second disadvantage, the executable's size, often simply doesn't matter at all.

Upvotes: 11

Lance Richardson
Lance Richardson

Reputation: 4610

I would recommend using -O2 most of the time, benefits include:

  • Usually reduces size of generated code (unlike -O3).
  • More warnings (some warnings require analysis that is only done during optimization)
  • Often measurably improved performance (which may not matter).

If release-level code will have optimization enabled, it's best to have optimization enabled throughout the development/test cycle.

Source-level debugging is more difficult with optimizations enabled, occasionally it is helpful to disable optimization when debugging a problem.

Upvotes: 27

Georg Schölly
Georg Schölly

Reputation: 126105

Always, except when you're programming and just want to test something you just wrote.

Upvotes: 5

Related Questions