[Solved] Benefits of ‘Optimize code’ option in Visual Studio build

Much of our C# release code is built with the ‘Optimize code’ option turned off. I believe this is to allow code built in Release mode to be debugged more easily.

Given that we are creating fairly simple desktop software which connects to backend Web Services, (ie. not a particularly processor-intensive application) then what if any sort of performance hit might be expected?

And is any particular platform likely to be worse affected?

Eg. multi-processor / 64 bit.

Enquirer: g t

Solution #1:

The full details are available at http://blogs.msdn.com/jaybaz_ms/archive/2004/06/28/168314.aspx.

In brief…

In managed code, the JITter in the runtime does nearly all the optimization. The difference in generated IL from this flag is pretty small.

Respondent: Zian Choy
Solution #2:

You are the only person who can answer the “performance hit” question. Try it both ways, measure the performance, and see what happens. The hit could be enormous or it could be nonexistant; no one reading this knows whether “enormous” to you means one microsecond or twenty minutes.

If you’re interested in what optimizations are done by the C# compiler — rather than the jitter — when the optimize switch is on, see:


Respondent: Eric Lippert
Solution #3:

In fact, there is a difference, sometimes quite significant. What can really affect the performance (as it is something that JIT does not fully take care of):

  • Unnecessary local variables (i.e., bigger stack frames for each call)
  • Too generic conditional instructions, JIT translates them in quite a straightforward manner.
  • Unnecessary branching (also not served well by a JIT – after all, it does not have too much time to do all the smart optimisations)

    So, if you’re doing something numerical – turn on the optimisation. Otherwise you won’t see any difference at all.

Respondent: SK-logic
Solution #4:

The optimizations done by the compiler are fairly low level and shouldn’t affect your users’ experience.

If you’d like to quantify the optimization on your application, simply profile a non-optimized and an optimized build and compare the results.

Respondent: Ben S
Solution #5:

I find that with complex, CPU intensive code (the code i’m using is a Monte Carlo simulation that can spawn enough threads to 100% utilize a computer. This was tested in a 36 core environment) the performance hit can be up to 4 times higher! A simulation that takes 2 hours will take about 9 hours without the optimization flag. (the paths are about 500,000 and for each paths there are 500 steps for around 2000 different objects with highly complex calculation on each objects).

Respondent: Alex Lin
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.