44

I'm compiling a program with -O3 for performance and -g for debug symbols (in case of crash I can use the core dump). One thing bothers me a lot, does the -g option results in a performance penalty? When I look on the output of the compilation with and without -g, I see that the output without -g is 80% smaller than the output of the compilation with -g. If the extra space goes for the debug symbols, I don't care about it (I guess) since this part is not used during runtime. But if for each instruction in the compilation output without -g I need to do 4 more instructions in the compilation output with -g than I certainly prefer to stop using -g option even at the cost of not being able to process core dumps.

How to know the size of the debug symbols section inside the program and in general does compilation with -g creates a program which runs slower than the same code compiled without -g?

juanchopanza
  • 210,243
  • 27
  • 363
  • 452
e271p314
  • 3,311
  • 6
  • 31
  • 49
  • 1
    Did you benchmark it? – Shiplu Mokaddim Jun 09 '14 at 09:15
  • 4
    It is not easy to benchmark this program, really, if it was easy to do I wouldn't ask this question, I would measure the penalty myself. – e271p314 Jun 09 '14 at 09:39
  • 3
    I have measured it. It seems they runs same speed. `-g` just adds debugging symbols. Obviously it depends on your program. – Shiplu Mokaddim Jun 09 '14 at 09:47
  • 4
    MSVC uses a separate [pdb file](http://en.wikipedia.org/wiki/Program_database) to store debug symbols and you can debug even in release mode so sometimes you even face a variable that is optimized out and it's value is undefined. I don't know if gcc has a similar option – phuclv Jun 09 '14 at 10:00

1 Answers1

64

Citing from the gcc documentation

GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values are already at hand; some statements may execute in different places because they have been moved out of loops.

that means:

I will insert debugging symbols for you but I won't try to retain them if an optimization pass screws them out, you'll have to deal with that

Debugging symbols aren't written into the code but into another section called "debug section" which isn't even loaded at runtime (only by a debugger). That means: no code changes. You shouldn't notice any performance difference in code execution speed but you might experience some slowness if the loader needs to deal with the larger binary or if it takes into account the increased binary size somehow. You will probably have to benchmark the app yourself to be 100% sure in your specific case.

Notice that there's also another option from gcc 4.8:

-Og

Optimize debugging experience. -Og enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.

This flag will impact performance because it will disable any optimization pass that would interfere with debugging infos.

Finally, it might even happen that some optimizations are better suited to a specific architecture rather than another one and unless instructed to do so for your specific processor (see march/mtune options for your architecture), in O3 gcc will do its best for a generic architecture. That means you might even experience O3 being slower than O2 in some contrived scenarios. "Best-effort" doesn't always mean "the best available".

200_success
  • 6,669
  • 1
  • 36
  • 68
Marco A.
  • 41,192
  • 25
  • 117
  • 233
  • 1
    FWIW you could even specify how to achieve that, i.e `march` and `mtune`. – edmz Jun 09 '14 at 09:39
  • 6
    Note that even if you do notice performance differences due to `-g`, you can use `objcopy --strip-debug` and `objcopy --only-keep-debug` to move the debug info to a separate file. –  Jun 09 '14 at 09:41