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

Deadly 提交于 2019-11-26 12:40:03

问题


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

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:

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:

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

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?


回答1:


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

According to those:

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

    • /GF eliminates duplicate strings
    • /Gy does function level linking

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.




回答2:


I found it here:

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



来源:https://stackoverflow.com/questions/5063334/what-is-the-difference-between-the-ox-and-o2-compiler-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!