C++ /Ox 和 /O2 编译器选项之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5063334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:18:14  来源:igfitidea点击:

What is the difference between the /Ox and /O2 compiler options?

c++visual-studiovisual-c++compiler-optimizationcompiler-options

提问by Cody Gray

Microsoft's C++ compiler (cl.exe, as included with Visual Studio) offers several optimization switches. The difference between most of them seems self-explanatory, but it's not clear to me what the difference is between /O2(which optimizes code for maximum speed) and /Ox(which selects "full optimization").

Microsoft 的 C++ 编译器(cl.exe包含在 Visual Studio 中)提供了几个优化开关。它们中的大多数之间的区别似乎不言自明,但我不清楚/O2(优化代码以获得最大速度)和/Ox(选择“完全优化”)之间的区别是什么。

I've tried reading the documentationfor the /Oxoption, and it seems to confirm that this switch also enables optimizations for maximum speed, rather than size:

我试着阅读文档/Ox选项,它似乎证实,该交换机还支持优化的最高速度,而不是大小:

The /Oxcompiler option produces code that favors execution speed over smaller size.

所述/Ox编译器选项产生在较小尺寸有利于代码的执行速度。

But in particular, the following statement under the "Remarks" section caught my eye:

但特别是,“备注”部分下的以下声明引起了我的注意:

In general, specify /O2(Maximize Speed) instead of /Ox.

通常,指定/O2(Maximize Speed) 而不是/Ox

So my question is, why should one generally favor /O2over /Ox?Does the latter option enable a particular optimization known to cause unforeseen bugs or otherwise unexpected behavior? Is it simply that the amount of optimization to be gained is not worth the additional compile time? Or is this just a completely meaningless "recommendation" resulting from the fact that /O2is the defaultoption in VS?

所以我的问题是,为什么要一个普遍青睐/O2/Ox后一个选项是否启用了已知会导致不可预见的错误或其他意外行为的特定优化?是否只是要获得的优化量不值得额外的编译时间?或者这只是一个完全没有意义的“推荐”,因为它/O2是VS 中的默认选项?

采纳答案by Alastair Maw

Asha's answercites a blog post about Visual Studio 2005, and is rather out of date.

Asha 的回答引用了一篇关于 Visual Studio 2005 的博客文章,并且已经过时了。

The latest version of the documentation is available here:

最新版本的文档可在此处获得:

According to those:

根据那些:

You may additionally be interested in /GS-which turns off security checks around the stack, which can be a significant performance hit (see the MS docs for /GS).

您可能还对/GS-关闭堆栈周围的安全检查感兴趣,这可能会严重影响性能(请参阅/GSMS 文档)。

You should benchmark your specific application, as ever.

您应该一如既往地对您的特定应用程序进行基准测试。

回答by Asha

I found it here:

我在这里找到

Ox and O2 are almost identical. They differ only in the fact that O2 also throws GFand Gy. There is almost no reason to avoid throwing these two switches.

Ox 和 O2 几乎相同。他们的区别仅在于 O2 也投掷了GFGy。几乎没有理由避免抛出这两个开关。