问题
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 ?
回答1:
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):
/Og
enables global optimizations/Oi
generates intrinsic functions for appropriate function calls./Ot
(a default setting) tells the compiler to favor optimizations for speed over optimizations for size./Oy
suppresses the creation of frame pointers on the call stack for quicker function calls./Ob2
expands functions marked asinline
or__inline
and any other function that the compiler chooses
The /G options are:
/GF
(Eliminate Duplicate Strings)/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.
回答2:
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.
来源:https://stackoverflow.com/questions/3780495/in-vc-what-is-the-pragma-equivalent-of-o2-compiler-option-optimize-for-spee