Optimizing Your Project Code

Menu:  Project -> [Project Name] Properties
Versions:  2008,2010
Published:  10/28/2010
Code:  vstipProj0014

 

 

Optimization in Project Properties

In vstipDebug0032 we discussed optimization.  Just to recap, when optimization is turned off (the default setting for Debug builds) then that factors into the code being considered "yours" for the purposes of determining what is "Just My Code".  As a rule you won't turn this on when doing your Debug builds. 

 

Also, when you do a Release build then optimization is turned on by default.  So what is optimization?  According to the documentation, the optimization "option enables or disables optimizations performed by the compiler to make your output file smaller, faster, and more efficient".  Let's look at where this option is located:

 

C#

In C# this is found in the Project properties on the Build tab under "Optimize code":

image

 

 

VB

In VB, it is also in the Project properties but on the Compile tab and you have to click the "Advanced Compile Options" button:

image

 

Then locate the "Enable optimizations" option:

image

 

 

Optimization Explained

There are a ton of optimizations that you can do such as optimizing for application speed or size of your program.  Enabling optimizations from project properties will enable full optimization (/Ox).  You can get a sense of the full list of features here in this list from https://msdn.microsoft.com/en-us/library/k1ack8f1.aspx:

  • /O1 optimizes code for minimum size.

  • /O2 optimizes code for maximum speed.

  • /Ob controls inline function expansion.

  • /Od disables optimization, speeding compilation and simplifying debugging.

  • /Og enables global optimizations.

  • /Oi generates intrinsic functions for appropriate function calls.

  • /Os tells the compiler to favor optimizations for size over optimizations for speed.

  • /Ot (a default setting) tells the compiler to favor optimizations for speed over optimizations for size.

  • /Ox selects full optimization.

  • /Oy suppresses the creation of frame pointers on the call stack for quicker function calls.

     

    Basically, it does a lot of cool things that are great for a shipping application but not for one that you are currently working on and debugging.  At this point we can get pretty deep into what is actually going on with the optimizations.  There is an excellent article on this by Eric Lippert (Microsoft) that can be found here:  https://blogs.msdn.com/b/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx

     

    Here are some of the highlights from the article that gives an idea of the optimizations:

    * Omit code for things like int foo = 0; because the memory allocator will initialize fields to default values.

    * If you have a local which is never used at all, then there is no storage allocated for it.

    * The compiler is more aggressive about generating code that throws away "temporary" values quickly for things like controlling variables of switch statements, the condition in an "if" statement, the value being returned, and so on.