为什么托管模块比C#中的非托管模块快? [英] Why Managed module Is faster than Unmanaged module in C#

查看:328
本文介绍了为什么托管模块比C#中的非托管模块快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在书籍作家中读一本书(CLR通过c#)讲述IL和管理模块,管理模块编译在本机cpu代码,然后在书的作者说,管理模块的速度比非托管module.my问题是为什么管理模块比非管理模块快,但在管理模块中首先编译为IL,然后在本机cpu中编译。

I reading a book( CLR via c#)in the book writer speak about IL and managed module that managed module compiling in native cpu code and then execute in the book writer said that Managed module Is faster than Unmanaged module.my question is why Managed module Is faster than Unmanaged module but for in Managed module first compile to IL and then compile in native cpu.

推荐答案

我在下面写的不是我的,它是从一本书复制粘贴的: CLR via C#,Page No 14

(What I wrote below is not mine, it is copy-pasted from a book: CLR via C#, Page No 14)


当JIT编译器在
运行时将IL代码编译成本地机器代码时,编译器比非管理编译器知道的执行环境更多地了解

When the JIT compiler compiles the IL code into native machine code at run time, the compiler knows more about the execution environment than an unmanaged compiler would know.

托管代码可以胜过非托管代码

Some ways in which managed code can outperform unmanaged code


  • JIT编译器可以确定应用程序是否在
    Intel Pentium 4 CPU上运行,优点是CPU提供的任何特殊指令的
    。通常,非托管的
    应用程序被编译为CPU
    的最低公分母,并避免使用将给予应用程序
    性能提升的特殊指令。

  • JIT编译器可以确定某个测试在运行的计算机上总是失败。考虑示例

  • A JIT compiler can determine if the application is running on an Intel Pentium 4 CPU and produce native code that takes the advantage of any special instruction offered by the CPU. Usually, unmanaged application are compiled for the lowest-common denominator of the CPU and avoid using special instruction that would give application performance boost.
  • A JIT compiler can determine when a certain test always fail on the machine that it is running on. Consider the example

if (num_of_cpu > 1)
{        
}


此代码导致JIT不生成任何CPU指令只有一个CPU。

This code causes the JIT to not generate any CPU instruction if the machine has only One CPU.


  • CLR可以分析代码的执行,并在应用程序运行时将IL重新编译为本机代码。根据观察到的执行模式,将重新编译重新编译的代码以减少不正确的分支预测。 C#的当前版本不会这样做,但将来的版本可能会。

这篇关于为什么托管模块比C#中的非托管模块快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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