title: Does the VS2008 C++ optimizer sometimes produce slower code?
body: <p>Following on from a <a href="https://stackoverflow.com/questions/5165877/whole-program-optimization-failing-in-vc2008">previous question</a>, I've been playing around with optimizer settings in my release build to see what benefits are to be gleaned from using compiler optimization.  Up until now, I've been using /Ob1 (only inline where inline is explicitly given), and /Oi (Enable intrinsic functions).  I tried changing this to include /Ot (favour fast code), /Oy (omit frame pointers) and /Ob2 (inline any suitable), and to my surprise a regression suite that was taking 2h58 minutes now took 3h16m.  My first assumption was that my own inlining was more aggressive than the compiler, but moving back from /Ob2 to /Ob1 only improved things to 3h12m.  I'm still running more tests, but it would appear that in some cases /Ot (favour fast code) is actually slowing things down.  The software is multi-threaded and computation intensive (surface modelling, manipulation and visualisation), and has already been heavily manually optimized based on profiler results.  The program is also dealing with large amounts of data, and uses #pragma pack(4) pretty regularly.</p>
  
  <p>So the questions is this.  For a manually optimized program is compiler optimization in VS2008 liable to do more damage than good?  Put another way, are there known documented scenarios where compiler optimization reduces performance? (n.b. profiling the compiler optimized code is painful, hence profiling to date has been done on unoptimized code).</p>
  
  <p><strong>Edit</strong>  As per Cody Gray's and others suggestions, I have added /O2 to the optimization settings and re-executed my test suite.  This resulted in a run time of 3h01, which was comparable to the minimally optimized run.  Given the (slightly dated) <a href="http://msdn.microsoft.com/en-us/library/aa290055(v=vs.71).aspx" rel="nofollow noreferrer">MSDN guide lines on optimization</a> and post from GOZ, I'm going to check /O1 to see if smaller is actually faster in my case.  Note the current EXE file is about ~11mb.  I'll also try and get a VS2010 build together and see how that fares.  </p>
  
  <p><strong>Edit2</strong> With /O1, the run time was 3h00, and the 11mb exe was 62k smaller.  Note that the reason behind this post, and the previous linked one, were to check whether the benefits of turning on compiler optimizations outweighed the drawbacks in terms of profiling and debugging.  In this specific instance, they appear not to be, although I admit to being surprised that none of the combinations tried added any benefit and some visibly reduced performance.  FWIW, as per this <a href="https://stackoverflow.com/questions/385506/when-is-optimisation-premature/385825#385825">previous thread</a>, I tend to do most of my optimization at design time and use the profiler primarily to check design assumptions, I reckon I'll be sticking with this approach.  I'll have one final go on VS2010 with whole program optimization enabled and leave it at that.</p>
  
  <p>Thanks for all the feedback!</p>