Is there a runtime benefit to using const local variables?(使用 const 局部变量有运行时的好处吗?)
问题描述
除了确保它们不能被更改(根据编译器错误的调整)之外,JIT 是否对 const locals 进行任何优化?
Outside of the ensuring that they cannot be changed (to the tune of a compiler error), does the JIT make any optimisations for const locals?
例如.
推荐答案
生成的IL不一样(使用Release模式):
The generated IL is different (using Release mode):
如您所见,编译器将所有变量使用替换为常量的值,从而导致堆栈更小.
As you can see the compiler replaces all variable usages by the value of the constant which results in a smaller stack.
这篇关于使用 const 局部变量有运行时的好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!