
就像the documentation for the /optimize compiler flag这样一个例子
The /optimize option enables or disables optimizations performed by the compiler to make your output file smaller,faster,and more efficIEnt.
这意味着语言编译器至少应用一些优化.
不过,与Try Roslyn一起玩,似乎并非如此.看起来C#编译器根本没有优化.
例子
输入:
bool y = true;if (y) Console.Writeline("yo"); Decompiled output:
if (true){ Console.Writeline("yo");} 输入:
static voID Donothing() { }static voID Main(string[] args){ Donothing(); Console.Writeline("Hello World!");} Decompiled output:
private static voID Donothing(){}private static voID Main(string[] args){ normalProgram.Donothing(); Console.Writeline("Hello World!");} 输入:
try{ throw new Exception();}catch (Exception){ Console.Writeline("Hello World!");} Decompiled output:
try{ throw new Exception();}catch (Exception){ Console.Writeline("Hello World!");} 如您所见,C#语言编译器似乎完全没有优化.
这是真的?如果是这样,为什么文档声称/ optimize会使您的可执行文件更小?
解决方法 我可以想到的唯一的优化是C#编译器执行,你可以用反编译器实际看到的是添加一个空的静态构造函数.他们通常很无趣,只是更紧凑的IL.你只能看到它们,当你看着IL,一个体面的反编译器不会显示它.未优化的代码具有递归体系编译器的代码生成器的典型工件,紧随着相同变量的加载后的冗余存储分支到下一个地址.优化器知道如何消除它们.标准示例是发出的nop以帮助调试,它们允许您在大括号上设置断点.由优化器删除
没有什么可以提供一个容易观察到的perf改进,虽然你可能会幸运的是,更紧凑的IL刚刚给抖动优化器足够的时间来消除关键的内存存储.这不经常发生.
总结以上是内存溢出为你收集整理的C#语言编译器是否自行执行任何实际的优化?全部内容,希望文章能够帮你解决C#语言编译器是否自行执行任何实际的优化?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)