/ Ox和/ O2编译器选项有什么区别? [英] What is the difference between the /Ox and /O2 compiler options?

查看:931
本文介绍了/ Ox和/ O2编译器选项有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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").

我试过读取文档 / Ox 选项,似乎确认此开关也可以优化最大速度,而不是大小:

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


/ Ox 编译器选项生成的代码在更小的大小上有利于执行速度。

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

注释部分引起了我的注意:

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


一般来说,指定 / O2 (最大化速度),而不是 / Ox

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

所以我的问题是,为什么一般来说 / O2 / Ox 后一个选项是否启用

a特定的优化已知会导致不可预见的错误或其他意外行为?是不是简单地说,要获得的优化量不值得额外的编译时间?或者,这只是一个完全无意义的推荐,因为 / O2 是VS中的默认选项?

So my question is, why should one generally favor /O2 over /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 /O2 is the default option in VS?

推荐答案

Asha的回答引用了有关Visual Studio的博文

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

最新版本的文档位于此处:

The latest version of the documentation is available here:

  • /Ox: https://msdn.microsoft.com/en-us/library/59a3b321.aspx
  • /O2: https://msdn.microsoft.com/en-us/library/8f8h5cxt.aspx

根据这些:


  • / Ox / Og / Oi / Ot / Oy / Ob2

  • / O2 →同样,但进一步添加 / Gs / GF / Gy

  • /Ox/Og /Oi /Ot /Oy /Ob2
  • /O2 → the same, but further adds /Gs /GF /Gy

  • /Gs controls stack checking calls
  • /GF eliminates duplicate strings
  • /Gy does function level linking

似乎 / O2 设定 / Gs 一个值,意味着默认值 / Gs0 。并从文档中为:

It seems that /O2 sets /Gs without a value, which implies a default value of /Gs0. And from the docs for that:


/ Gs0 调用需要
存储局部变量。这可能对
的性能产生负面影响。

/Gs0 activates stack probes for every function call that requires storage for local variables. This can have a negative impact on performance.

因此 / O2 可能实际上慢于 / Ox 为您,完全是因为。

So /O2 may well actually be slower than /Ox for you, solely due to that. IMO that's surprising enough to look like a bug (either in the docs or the implementation).

您还可以对 / GS-

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).

您应该对您的特定应用程序进行基准测试。

You should benchmark your specific application, as ever.

这篇关于/ Ox和/ O2编译器选项有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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