Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization in GCC

I have two questions:

(1) I learned somewhere that -O3 is not recommended with GCC, because

The -O3 optimization level may increase the speed of the resulting executable, but can also increase its size. Under some circumstances where these optimizations are not favorable, this option might actually make a program slower. in fact it should not be used system-wide with gcc 4.x. The behavior of gcc has changed significantly since version 3.x. In 3.x, -O3 has been shown to lead to marginally faster execution times over -O2, but this is no longer the case with gcc 4.x. Compiling all your packages with -O3 will result in larger binaries that require more memory, and will significantly increase the odds of compilation failure or unexpected program behavior (including errors). The downsides outweigh the benefits; remember the principle of diminishing returns. Using -O3 is not recommended for gcc 4.x.

Suppose I have a workstation (Kubuntu9.04) which has 128 GB of memory and 24 cores and is shared by many users, some of whom may run intensive programs using like 60 GB memory. Is -O2 a better choice for me than -O3?

(2) I also learned that when a running program crashes unexpectedly, any debugging information is better than none, so the use of -g is recommended for optimized programs, both for development and deployment. But when compiled with -ggdb3 together with -O2 or -O3, will it slow down the speed of execution? Assume I am still using the same workstation.

like image 941
Tim Avatar asked Sep 02 '09 16:09

Tim


People also ask

How many optimization levels are there in GCC?

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

Does GCC optimize assembly?

No. GCC passes your assembly source through the preprocessor and then to the assembler. At no time are any optimisations performed.

How do I disable GCC optimization?

Use the command-line option -O0 (-[capital o][zero]) to disable optimization, and -S to get assembly file. Look here to see more gcc command-line options. Show activity on this post.


2 Answers

  1. The only way to know for sure is to benchmark your application compiled with -O2 and -O3. Also there are some individual optimization options that -O3 includes and you can turn on and off individually. Concerning the warning about larger binaries, note that just comparing executable file sizes compiled with -O2 and -O3 will not do much good here, because it is the size of small critical internal loops that matters here the most. You really have to benchmark.

  2. It will result in a larger executable, but there shouldn't be any measurable slowdown.

like image 52
Laurynas Biveinis Avatar answered Oct 15 '22 15:10

Laurynas Biveinis


Try it
You can rarely make accurate judgments about speed and optimisation without any data.

ps. This will also tell you if it's worth the effort. How many milliseconds saved in a function used once at startup is worthwhile ?

like image 31
Martin Beckett Avatar answered Oct 15 '22 15:10

Martin Beckett