81

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?

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

2 Answers2

59

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.

Asha
  • 11,002
  • 6
  • 44
  • 66
  • Nice job! I guess I just didn't search quite long enough myself. Interesting that the documentation makes it appear that `/Ox` is a *higher* level of optimization than `/O2`. It seems that both of those would provide at least a theoretical performance boost. – Cody Gray - on strike Feb 21 '11 at 07:38
  • Apparently so.. It seems using the /OPT:REF linker option along with /Gy would help in removing the unreferenced functions from the final exe. I'm not too sure about how useful is /GF is though. – Asha Feb 21 '11 at 07:42
  • 16
    /GF is great - it pools all identical string literals in a read-only memory region. So if you have some literal in multiple areas of code you'll only have it once in the executable which reduces the image size. And if your code attempts to modify it you'll get Access Violation which protects you against dumb errors. – sharptooth Feb 21 '11 at 07:48
  • 8
    It's worth noting that, according to Stephan T Lavavej (the STL lead for MS VC++), [Microsoft's compiler backend team considers /Ox "an evil switch from beyond time"](https://github.com/ulfjack/ryu/pull/70#issuecomment-412168459) and urges everyone to use /O2 instead of /Ox. – Chris Kline Aug 20 '18 at 07:58
56

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:

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

You should benchmark your specific application, as ever.

Community
  • 1
  • 1
Alastair Maw
  • 5,373
  • 1
  • 38
  • 50
  • Wrt. your take away, that /O2 may imply /Gs0 ... see my "doubts": http://stackoverflow.com/questions/42342931/does-msvc-o2-imply-gs0-or-gs4096 – Martin Ba Feb 20 '17 at 11:08
  • 1
    /O2 does not appear to imply /Gs (i.e. /Gs0) in latest versions of Visual Studio: https://learn.microsoft.com/en-us/cpp/build/reference/o1-o2-minimize-size-maximize-speed – Chris Kline Aug 20 '18 at 07:50