为什么托管模块比C#中的非托管模块快?

编程入门 行业动态 更新时间:2024-10-26 22:22:08
本文介绍了为什么托管模块比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#中的非托管模块快?

本文发布于:2023-06-01 10:48:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/413470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!