基于随机Internet注释,我一直认为C#编译器对IL进行简单的优化(删除always-true if-statement,简单内联等),然后JIT执行真正的,复杂的优化.
就像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");
if (true) { Console.WriteLine("yo"); }
输入:
static void Donothing() { } static void Main(string[] args) { Donothing(); Console.WriteLine("Hello World!"); }
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!"); }
try { throw new Exception(); } catch (Exception) { Console.WriteLine("Hello World!"); }
如您所见,C#语言编译器似乎完全没有优化.
这是真的?如果是这样,为什么文档声称/ optimize会使您的可执行文件更小?
解决方法
原文地址:https://www.jb51.cc/csharp/93556.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。