托管程序优于非托管程序的地方

本文探讨了托管程序相较于非托管程序的优点,重点介绍了JIT编译器如何针对特定CPU进行优化,包括利用特殊指令集提高性能、根据运行时条件裁剪不必要的代码以及运行时代码优化等。

传统的C/C++编译生成的应用一般属于非托管应用程序,而基于.NET框架,通过CLR、JIT来编译生成的应用程序属于托管应用程序。

一般的理解,非托管程序肯定要优于托管程序的,为什么呢?因为非托管程序省略了CLR加载、JIT编译的过程,而直接编译成本地CPU执行的指令的。

那么托管程序较非托管程序,到底好在哪里呢?

最主要一点就是JIT编译器在将IL代码编译成本地CPU指令时,会获取更多的执行环境信息,这样它就能针对特定的CPU指令进行特定优化。

下面列出了托管程序较非托管程序的几个优点:

1.  A JIT compiler could detect that the application is running on a Pentium 4 and produce native code that takes advantage of any special instructions offered by the Pentium 4. Usually, unmanaged applications are compiled for the lowest−common−denominator CPU and avoid using special instructions that would give the application a performance boost over newer CPUs.

2. A JIT compiler could detect that a certain test is always false on the machine that it is running on. For example, consider a method with code like this:
if (numberOfCPUs > 1) {
Ã
}
This code could cause the JIT compiler not to generate any CPU instructions if the host machine has only one CPU. In this case, the native code has been fine−tuned for the host machine: the code is smaller and executes faster.
·
3. The CLR could profile the code’s execution and recompile the IL into native code while the application runs. The recompiled code could be reorganized to reduce incorrect branch predictions depending on the observed execution patterns.


PS:

如果觉得您生成的托管程序确实比非托管程序性能低下,那么MS也提供了一个工具--NGen.exe,用来将IL代码编译成本地代码并保存到文件,下次再运行的时候,CLR会直接加载该文件运行,省却了JIT的中间编译过程,这会提升不少的性能。

NGen.exe使用说明见: http://msdn.microsoft.com/zh-cn/library/6t9t5wcf.aspx

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值