The “Optimize code” checkbox in Visual Studio. Wha

2020-05-31 04:05发布

The Build tab of the Project Properties in Visual Studio 2005/2008 contains the "Optimize code".

The documentation states that it "... enables or disables optimizations performed by the compiler to make your output file smaller, faster, and more efficient."

  1. My question is why should I NOT have it on?
  2. Why is it not on by default?
  3. What does it actually do?

3条回答
迷人小祖宗
2楼-- · 2020-05-31 04:28

Have a look at the answers to What is /optimize C# compiler key intended for? They answer your questions (especially Noldorin's answer).

查看更多
放荡不羁爱自由
3楼-- · 2020-05-31 04:33
  1. You wouldn't want this on for a debug built, as it makes stepping through code harder as the actual code that is running may not properly reflect what you have written (since some lines will be optimized out)

  2. It is not on by default for DEBUG builds for the above reason, it should be enabled by default on release builds

  3. It performs optimizations such as dynamic inlining and removing unneeded local variables. Any sort of optimization that can be decided upon at compile time.

查看更多
Deceive 欺骗
4楼-- · 2020-05-31 04:45

Wikipedia has an article on compiler optimization that covers many of the basic types of optimizations.

You don't want to create optimized debug builds. Optimization affects the debugabbility of your code - some lines of code may be removed, some lines of code from different parts of a function or from different functions may be merged together, local variables may be folded together, and so on. This means when debugging your current line can appear to jump around randomly due to the code being reorganized and inspecting local variables can be very misleading - the space for a local might be reused when it is no longer needed and appear to give a jibberish result.

查看更多
登录 后发表回答