visual-studio Visual Studio 中的“优化代码”复选框。它究竟有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1314433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The "Optimize code" checkbox in Visual Studio. What exactly does it do?
提问by AngryHacker
The Build tab of the Project Properties in Visual Studio 2005/2008 contains the "Optimize code".
Visual Studio 2005/2008 中项目属性的生成选项卡包含“优化代码”。
The documentation states that it "... enables or disables optimizations performed by the compiler to make your output file smaller, faster, and more efficient."
该文档指出它“...启用或禁用编译器执行的优化,以使您的输出文件更小、更快、更高效。”
- My question is why should I NOT have it on?
- Why is it not on by default?
- What does it actually do?
- 我的问题是为什么我不应该戴它?
- 为什么默认不开启?
- 它实际上有什么作用?
回答by LorenVS
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)
It is not on by default for DEBUG builds for the above reason, it should be enabled by default on release builds
It performs optimizations such as dynamic inlining and removing unneeded local variables. Any sort of optimization that can be decided upon at compile time.
您不希望在构建调试时启用此功能,因为它会使代码单步执行变得更加困难,因为正在运行的实际代码可能无法正确反映您编写的内容(因为某些行将被优化掉)
由于上述原因,DEBUG 版本默认不启用它,它应该在发布版本中默认启用
它执行优化,例如动态内联和删除不需要的局部变量。可以在编译时决定的任何类型的优化。
回答by Michael
Wikipedia has an article on compiler optimizationthat 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.
您不想创建优化的调试版本。优化会影响代码的可调试性 - 某些代码行可能会被删除,来自函数不同部分或不同函数的某些代码行可能会合并在一起,局部变量可能会折叠在一起,等等。这意味着在调试当前行时,由于代码被重新组织并且检查局部变量可能会非常具有误导性,因此在调试当前行时可能会出现随机跳转 - 局部空间可能会在不再需要时被重用,并且似乎给出了一个乱七八糟的结果。
回答by adrianbanks
Have a look at the answers to What is /optimize C# compiler key intended for?They answer your questions (especially Noldorin's answer).
查看什么是 /optimize C# 编译器密钥的目的的答案?他们会回答你的问题(尤其是诺多林的回答)。

