3

According to msdn,

/O2 (Maximize Speed)

is equivalent to

/Og/Oi/Ot/Oy/Ob2/Gs/GF/Gy

and according to msdn again, the following pragma

#pragma optimize( "[optimization-list]", {on | off} )

uses the same letters in its "optimization-list" than the /O compiler option. Available letters for the pragma are:

  • g - Enable global optimizations.
  • p - Improve floating-point consistency.
  • s or t - Specify short or fast sequences of machine code.
  • y - Generate frame pointers on the program stack.

Which ones should I use to have the same meaning as /O2 ?

jumar
  • 5,360
  • 8
  • 46
  • 42

2 Answers2

3

The Microsoft Docs article /O1, /O2 (Minimize Size, Maximize Speed) says for Visual Studio 2017:

The /O1 and /O2 compiler options are a quick way to set several specific optimization options at once. The /O1 option sets the individual optimization options that create the smallest code in the majority of cases. The /O2 option sets the options that create the fastest code in the majority of cases. The /O2 option is the default for release builds. This table shows the specific options that are set by /O1 and /O2:

Option                   Equivalent to
/O1 (Minimize Size)     /Og /Os /Oy /Ob2 /GF /Gy
/O2 (Maximize Speed)    /Og /Oi /Ot /Oy /Ob2 /GF /Gy

From the Microsoft Docs article /O Options (Optimize Code):

  1. /Og enables global optimizations
  2. /Oi generates intrinsic functions for appropriate function calls.
  3. /Ot (a default setting) tells the compiler to favor optimizations for speed over optimizations for size.
  4. /Oy suppresses the creation of frame pointers on the call stack for quicker function calls.
  5. /Ob2 expands functions marked as inline or __inline and any other function that the compiler chooses

The /G options are:

  1. /GF (Eliminate Duplicate Strings)
  2. /Gy (Enable Function-Level Linking)

The /G options aren't strictly optimizations, so that leaves us with /Og and /Ot, plus #pragma intrinsic (for item 2 in the list), #pragma auto_inline (for item 5 in the list) and possibly #pragma inline_depth. See Microsoft Docs article Optimization Pragmas and Keywords

See also Microsoft Docs article /Ox (Enable Most Speed Optimizations) which indicates the /Ox option is similar to the /O2 option except that it does not turn on /GF nor /Gy. See as well What is the difference between the /Ox and /O2 compiler options?

The Microsoft Docs article Compiler options listed by category has a list of compiler options with links as to what they mean.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
bosmacs
  • 7,341
  • 4
  • 31
  • 31
0

I don't think there is a direct equivalent.

#pragma optimise("gty", off)

Should cancel for a file, most of the effect of /O2 at the project level, but

#pragma optimise("gty", on)

Just says "use the compiler switch", so you need /O2 or /Og /Ot /Oy.

I can't find pragmas for the /G parts and they do optimisation 'like' things.

Julian
  • 1,522
  • 11
  • 26