Benefits of 'Optimize code' option in Visual Studio build

烈酒焚心 提交于 2019-12-02 18:31:35

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.

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:

http://blogs.msdn.com/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx

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.

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.

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!