Don't spend time micromanaging optimization options in Visual C++
It's surprisingly common to see people ask what the best Visual C++ optimization options are for their C++ code.
The best answer is to profile and benchmark.
Failing that, it's pretty simple. Use /O2
to optimize for speed, and /O1
to optimize for size. That's (almost) it. If you have a large program you will probably benefit more from optimizing for size:
Generally speaking, small applications should be compiled with /O2, and large applications should be compiled with /O1 because very large applications can end up putting a lot of stress on the instruction cache of the processor, and this can lead to worse performance. To minimize this, use /O1 to reduce the amount of "code bloat" introduced by the optimizer from certain transformations such a loop unrolling or selection of larger, faster sequences of code. [MSDN]
But what about Enable Intrinsic Functions (/Oi
), Omit Frame Pointers (/Oy
), etc.?
Check out the documentation for /O2
and /O1
:
/O1
(Minimize Size) : Equivalent to /Og /Os /Oy /Ob2 /Gs /GF /Gy/O2
(Maximize Speed) : Equivalent to /Og /Oi /Ot /Oy /Ob2 /Gs /GF /Gy
They are already all specified. Yes, the property pages say Disabled or No, but what they really mean is Default.
The only general optimization I'm aware of that doesn't fall in this category is Whole Program Optimization (/GL
), which also requires Link Time Code Generation (/LTCG
).
Finally, what about Full Optimization (/Ox
)?
In general, /O2 should be preferred over /Ox and /O1 over /Oxs. [MSDN]
[Edit: Note that there is an IDE bug in Visual Studio 2008 which may affect whether or not optimizations are applied. I write more about it here.]