本文介绍了为什么要使用 -O2 而不是 -O3 进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们通常使用 -O2 进行编译,因为 -O3 会触发细微的错误".

We usually compile with -O2 because -O3 would "trigger subtle bugs".

对于我们的 GCC 版本,-O3 启用了更积极的内联,这实际上会揭示否则未被注意到的错误(例如,使用来自函数的未初始化值将它们作为引用参数或数组的越界访问).在我看来,这种激进的内联还允许使用更小的函数以更具表现力的方式进行编码,并且 -funswitch-loops 有助于使变量定义在循环中更加本地化.

For our GCC version -O3 enables more aggressive inlining which would actually reveal bugs otherwise unnoticed (e.g. use of uninitialized values from functions taking them as reference arguments or out-of-bounds access for arrays). It seems to me this aggressive inlining also allows a more expressive way of coding with smaller functions and -funswitch-loops helps keeping variable definitions more local in loops.

鉴于我们代码中的错误比编译器错误更可能出现几个数量级,并且我们使用 -Wall -Wextra 没有任何问题,我们应该寻找什么样的错误?

Given that bugs in our code are orders of magnitude more likely than compiler bugs and that we use -Wall -Wextra without any issues what kind of bugs should we be looking for?

如果重要,我们使用 gcc-4.3.2.编译时间对我们来说不是主要问题.

If it matters we use gcc-4.3.2. Compile time is not a major issue for us.

推荐答案

Size.当然,如果大小确实很重要(有时很重要,比如嵌入式),那么可以使用 -Os.但 O3 的主要区别在于(您已经提到过)内联.这可以增加生成的代码大小(但速度更快).也许您想要速度,但根本不想要(空间)成本?否则我认为没有理由不使用 O3(除非您知道 gcc 编译器错误仅出现在您的 O3 代码中,但只要您没有错误,您就无法在 O2 重现,我不在乎).

Size. Of course if size does really matters (sometimes is does, like embedded), one would use -Os. But main difference at O3 is the (from you already mentioned) inlining. This can increase the generated code size (but it is faster). Maybe you want speed, but not at all (space) cost? Otherwise I would see no reason why not to use O3 (except you know of a gcc compiler bug that only occurs in your code at O3, but as long as you dont have an error, you cant reproduce at O2, I would not care).

这篇关于为什么要使用 -O2 而不是 -O3 进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 23:35