如果用-g0 编译的代码比-g3 快?

If code compiled with -g0 is faster than -g3?

我只为自己使用我的程序。 我是否应该始终使用 -g3(调试最大值)编译它,因为有时我需要调试它?

如果使用 -g0(调试 none),与 -g3(调试最大值)相比,我的程序执行速度更快?

关于-gLEVEL(来自gcc manual):

Request debugging information and also use level to specify how much information. The default level is 2.

Level 0 produces no debug information at all. Thus, -g0 negates -g.

Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. This includes descriptions of functions and external variables, and line number tables, but no information about local variables.

Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.

因此 -g0-g3 之间的区别在于,级别 0 没有调试符号,级别 3 则有很多符号。

无论如何,调试符号位于与 code/data 部分完全不同的部分。您可以使用 objdump 查看(您可以阅读 How do debug symbols affect performance of a Linux executable compiled by GCC?)。

严格来说 -g0 你不应该得到一个更快的程序但是有很多调试符号程序加载时间可能会更长。

有趣的是,对于 GCC,调试符号 (-g) 的存在和优化级别 (例如 -O2) 是正交的,您可以使用 -g -O2 而不会丢失编译器优化(你得到的调试信息不​​太有用,因为优化后的代码与原始源代码不太相似)。

我还会考虑 -Og 优化级别(在 GCC 4.8 中引入):

A new general optimization level, -Og, has been introduced. It addresses the need for fast compilation and a superior debugging experience while providing a reasonable level of runtime performance. Overall experience for development should be better than the default optimization level -O0.