为什么要用-O2代替-O3编译 [英] Why would one ever want to compile with -O2 instead of -O3

查看:445
本文介绍了为什么要用-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.

推荐答案

大小。当然,如果大小真的很重要(有时候确实如嵌入式),可以使用 -Os 。但是O3的主要区别在于(从你已经提到的)内联。这可以增加生成的代码大小(但速度更快)。也许你想要速度,但不是(空间)成本?否则,我会看不到任何理由不使用O3(除非你知道只有在O3的代码中才会出现的gcc编译器错误,但只要你没有错误,你不能在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编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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