简体   繁体   English

gcc -g调试标志是否会影响程序执行?

[英]Does the gcc -g debugging flag affect program execution?

I've just been testing a program I'm working, and I see that it's executing 3μs faster (a statistically significant change) when I compile it with -g. 我刚刚测试了一个我正在工作的程序,当我用-g编译它时,我发现它的执行速度提高了3μs(一个统计上显着的变化)。 This makes no sense to me - I thought that the -g flag wasn't supposed to affect the program execution, and that even if it did it would make it run slower, not faster. 这对我来说毫无意义 - 我认为-g标志不应该影响程序执行,即使它确实这样做也会使它运行得更慢,而不是更快。

Can anyone tell me why this is happening? 谁能告诉我为什么会这样? And whether it changes the programs execution flow? 它是否改变了程序的执行流程? I am not compiling with -O because I need it to execute exactly as written, but if -g can somehow make it run faster with changing the instruction order I should obviously be using that. 我没有使用-O进行编译,因为我需要它完全按照写入执行,但是如果-g可以通过改变指令顺序以某种方式使它运行得更快我显然应该使用它。

So I need to know exactly what changes the -g flag makes to the program. 所以我需要确切知道-g标志对程序的更改。

Edit: The more tests I run, the bigger the t-value gets (= the more statistically significant the difference becomes). 编辑:我运行的测试越多,t值越大(=差异变得越有统计意义)。 This is definitely not measurement error - something is going on. 这绝对不是测量误差 - 事情正在发生。

As others have said, debugging symbols will not change the control flow of your code unless that there is an (unlikely) bug in the compiler. 正如其他人所说,调试符号不会改变代码的控制流,除非编译器中存在(不太可能的)错误。

It changes execution, though, because the executable becomes bigger, and the executed code is spread more widely on more pages. 但是,它改变了执行,因为可执行文件变得更大,并且执行的代码在更多页面上更广泛地传播。 You can expect more cache misses and IO signals. 您可以预期更多缓存未命中和IO信号。 On a multi-tasking environment (and even a Linux/busybox system is such a thing) this can result is slightly different scheduling behavior. 在多任务环境(甚至Linux / busybox系统就是这样的事情)上,这可能导致稍微不同的调度行为。

On the other hand, measuring such tiny time differences as you describe them is an art in its own rights. 另一方面,如你所描述的那样,测量这种微小的时间差异本身就是一种艺术。 You are probably in an Heisenberg setting, where your measurements influence execution times. 您可能处于海森堡环境中,测量会影响执行时间。 Your measurements may show statistically significant deviation, but I would be extremely careful in interpreting them as saying such and such option makes faster code. 您的测量结果可能会显示出统计上显着的偏差,但我会非常小心地将它们解释为这样且此类选项会使代码更快。

The -g flag makes 0 changes to the actual generated code. -g标志对实际生成的代码进行0更改。 What it does is add debug sections to the executable. 它的作用是为可执行文件添加调试部分。 Those sections are not loaded at runtime, but debuggers can load them. 这些部分不会在运行时加载,但调试器可以加载它们。 As the executable now is a bit different it's larger - you might try to measure the no. 由于可执行文件现在有点不同,它更大 - 你可能会尝试测量no。 of page faults going on with one version vs another., there will be changes in how the executable is stored on disk, but no code changes. 在一个版本与另一个版本之间发生页面错误的情况下,可执行文件存储在磁盘上的方式会发生变化,但代码没有变化。

If you want to see the assembly, run objdump -d on your binary and compare 如果要查看程序集,请在二进制文件上运行objdump -d并进行比较

I do question the validity of the 3us increase though, reliably measuring 3us, at least on a general purpose OS is a hard task - I hope you have run your program a few thousand times (likely a few hundred thousand times rather) to come up with that number to try to eliminate all the random things affecting such a measurement. 我确实质疑3us增加的有效性,可靠地测量3us,至少在通用操作系统上是一项艰巨的任务 - 我希望你运行你的程序几千次(可能是几十万次)来用这个数字来试图消除影响这种测量的所有随机事物。

当我在其中一个子程序上使用-debug和-g标志时,我的代码得到了不同的答案,所以虽然我不知道为什么,是的,它肯定会影响程序的执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM