什么是“优化代码"?选项真的在 Visual Studio 中做吗? [英] What does "Optimize Code" option really do in Visual Studio?

查看:26
本文介绍了什么是“优化代码"?选项真的在 Visual Studio 中做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选项的名称说明了一些东西,但 Visual Studio/编译器真正做了什么以及真正的后果是什么?

Name of the option tells something but what Visual Studio/compiler really do and what are the real consequences?

如果你搜索谷歌你可以找到这个地址,但这并不是我真正要寻找的.我想知道真实发生的事情.例如为什么循环得到的时间更少,等等.

If you search google you can find this address, but that is not really I am looking for. I wonder the real things happening. For example why do the loops get less time, etc.

推荐答案

如果没有优化,编译器会生成非常愚蠢的代码 - 每个命令都以非常直接的方式编译,以便它执行预期的操作.默认情况下,Debug 版本禁用优化,因为如果没有优化,生成的可执行文件会以直接的方式与源代码匹配.

Without optimizations the compiler produces very dumb code - each command is compiled in a very straightforward manner, so that it does the intended thing. The Debug builds have optimizations disabled by default, because without the optimizations the produced executable matches the source code in a straightforward manner.

一旦您打开优化,编译器就会应用许多不同的技术来使代码运行得更快,同时仍然做同样的事情.Visual C++ 中优化和未优化构建之间最明显的区别是变量值在优化构建中尽可能长时间地保存在寄存器中,而在没有优化的情况下,它们总是存储在内存中.这不仅会影响代码速度,还会影响调试.由于这种优化,调试器无法在您单步执行代码时可靠地获取变量值.

Once you turn on the optimizations, the compiler applies many different techniques to make the code run faster while still doing the same thing. The most obvious difference between optimized and unoptimized builds in Visual C++ is the fact the variable values are kept in registers as long as possible in optimized builds, while without optimizations they are always stored into the memory. This affects not only the code speed, but it also affects debugging. As a result of this optimization the debugger cannot reliably obtain a variable value as you are stepping through the code.

编译器应用了多种其他优化,如/O 选项中所述(优化代码)MSDN 文档.有关各种优化技术的一般说明,请参阅维基百科编译器优化文章.

There are multiple other optimizations applied by the compiler, as described in /O Options (Optimize Code) MSDN docs. For a general description of various optimizations techniques see Wikipedia Compiler Optimization article.

这篇关于什么是“优化代码"?选项真的在 Visual Studio 中做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆