用-g本身编译是否会降低性能? [英] Does compiling with -g, in itself, degrade performance?

查看:499
本文介绍了用-g本身编译是否会降低性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是有关gcc和clang的问题,但可能适用于其他编译器。)

(This is a question about gcc and clang, but might apply to other compilers.)

如果我编译C或C ++代码,并使用生成调试信息 -g 开关本身是否以任何方式降低了编译程序的性能...

If I compile my C or C++ code, and generate debug info using the -g switch, does this in itself degrade performance of the compiled program in any way...


  1. 最优化( -O0 )?

  2. 最大优化( -O3 )?

  1. With minimum optimization (-O0)?
  2. With maximum optimization (-O3)?

注意:我的意思不是要分析/加载可执行文件会降低性能,这是由于额外的内容所致;我的意思是运行的代码。

Note: I don't mean the performance penalty of having to parse/load the executable, which is larger due to the extra content; I mean the code that's run.

推荐答案

我认为没有任何性能差异。确实,生成的代码是相同的,根据文档 -g 可与 -O 一起使用。 https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html rel = noreferrer>此处。此外,调试符号不是写在代码中,而是写到另一个称为调试部分的部分,该部分甚至在运行时也不会加载(仅由调试器加载)

I do not think there is any performance difference. Indeed the generated code would be the same and -g is usable with -O according to the documentation here. Besides, 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)

-g 不会更改运行的优化或生成的代码。
这是此处所述的gcc政策

-g will not change what optimizations are run, or code generated. This is gcc policy as stated here

不过,同样的文档指出:

However it might be useful to note that the same documentation states that:


您声明的某些变量可能根本不存在;控制流可能会使
暂时移至您未曾预期的位置;有些语句可能不执行
,因为它们计算的是恒定结果,或者它们的值已经在手边
了;某些语句可能在
的不同位置执行,因为它们已移出循环。不过,可以
调试优化的输出。因此,对于可能存在错误的程序使用
优化器是合理的。

The shortcuts taken by optimized code may occasionally be surprising: 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. Nevertheless it is possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.

因此最终调试永远不会会损害您的优化效果,但相反的结果是错误的,使用 -O3 可能会降低调试信息的质量(例如,删除无用的变量)。

So in the end the debugging will never hurt your optimisation but the opposite is false and using -O3 might degrade your debugging information (by deleting useless variables for instance).

请注意,在这种情况下,最好使用 -Og (如此处),因为它将:

Note that it may be better in that case to use -Og (as stated here) since it will:


优化调试体验。 -Og启用不会
干扰调试的优化。对于标准的edit-compile-debug周期,应该选择
的优化级别,在保持快速编译
和良好调试体验的同时,为
提供合理的优化级别。

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.

但是这会影响性能,因为某些优化过程会干扰调试。

However this would impact performance because some optimisation passes that would interfere with the debugging would not be done.

编辑:

链接和引号回答了您对<$ c的问题$ c> gcc 。它可能不适用于其他编译器,例如 clang
但是我也找到了 clang 的一些文档。
例如,此处

The links and quotes answer your question for gcc.It might not apply to other compilers such as clang. However I have found some documentation as well for clang. For instance here:


基本上,调试信息使您可以用
-O0 -g编译程序并获取完整的调试信息,从而使您可以
任意修改程序从调试器执行。用 -O3 -g编译程序
会为您提供完整的调试信息,这些信息始终是
可用的并且可以准确读取(例如,尽管消除了尾部调用和内联,您仍获得了准确的堆栈
跟踪) ,但是您可能会失去
修改程序和调用功能的能力,这些功能已被
从程序中优化或完全内联。

Basically, the debug information allows you to compile a program with "-O0 -g" and get full debug information, allowing you to arbitrarily modify the program as it executes from a debugger. Compiling a program with "-O3 -g" gives you full debug information that is always available and accurate for reading (e.g., you get accurate stack traces despite tail call elimination and inlining), but you might lose the ability to modify the program and call functions where were optimized out of the program, or inlined away completely.

这篇关于用-g本身编译是否会降低性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆