为什么.NET 代码编译为 MSIL?

发布于 2024-08-16 04:21:58 字数 69 浏览 6 评论 0原文

首先,.NET 代码编译为 MSIL,然后 JIT 将其转换为机器相关代码。任何人都可以告诉我两步编译带来的所有好处吗?谢谢

First .NET code compiles to MSIL and then JIT convert it to machine dependent code. Can any one tell me what all benifits get because of the 2 step compilation.Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

姜生凉生 2024-08-23 04:21:58

有几个原因。首先也是最重要的可能是使其跨平台。如果 C# 或其他 .NET 语言直接编译为本机代码,则必须针对其运行的每个平台重新编译它们。使用VM,所有代码都可以以中间格式保存,您只需为每个平台编写VM实现即可。

此外,通过使用与语言无关的中间语言,您可以使用许多高级语言(C#、VB.NET、Python 等)来引用用其他语言编写的程序集。由于它们都编译成相同的东西,因此它们可以彼此无缝地工作。

还有性能优势。 JIT 编译器可以专门针对当时运行代码的机器进行积极的优化。我不知道 .NET JIT 编译器在这个意义上做了多少优化,但理论上可以带来很大的好处。

There are a few reasons. First and foremost is probably to make it cross platform. If C# or other .NET languages compiled directly to native code, they would have to be recompiled for each platform they run on. With a VM, all code can be kept in an intermediate format, and you only need write a VM implementation for each platform.

Also, by having a language-agnostic intermediary language, you can have many high level languages (C#, VB.NET, Python etc) all referencing assemblies written in other languages. Since they all compile into the same thing, they can work seamlessly with each other.

There are also performance benefits. The JIT compiler can do aggressive optimizations specifically for the machine the code is running on at that time. I do not know how much optimization the .NET JIT compiler does in this sense, but there are very large theoretical benefits that could be had.

橙味迷妹 2024-08-23 04:21:58

答案可以在 MSDN 找到

The answer can be found at MSDN

月光色 2024-08-23 04:21:58

首先从高级语言转换到机器级,这就是.Net平台的设计方式。第一层负责 MSIL 的高级语言,第二层可以专注于挂接和挂接。从 MSIL 转换为机器级代码的平台故障。它主要支持语言互操作性,并且可能在不久的将来,当像 Mono 这样的项目获得更多支持时,它还将提供跨平台支持。

First Conversion from High Level Language and then to Machine Level, this is how .Net platform is designed. The first layer take care of High Level language to MSIL and second level can concentrate on hitch & glitch of platform to convert from MSIL to machine level code. It mainly supports Language interoperability and may be in near future it will also provide Cross Platform support when Project like Mono will gain more ground.

野の 2024-08-23 04:21:58
  • 可执行文件未绑定到平台。例如,XNA 同时针对 PPC (Xbox360) 和 x86 处理器。有些程序可以在 Linux 或 OSX 上的 Mono 上运行。

  • 它可以让您更好地优化目标机器或替换缺失的功能:

    • 例如,OSX >= 10.5 在运行时使用 OpenCL 编译缺少的 GPU 指令。
    • 假设您正在使用不支持浮点的 CPU,那么您可以使用 JIT 对其进行模拟,而无需重写完整的代码。
    • 在未来的某个时候,可以动态地将处理卸载到 GPU 或其他目标(我怀疑函数式语言更适合这种情况)。
  • An executable is not bound to the platform. For instance XNA targets both PPC (Xbox360) and x86 processors. Some programs will run on Mono on linux or OSX.

  • It allows you to better optimize for the target machine or replace missing functions:

    • For instance OSX >= 10.5 compiles in missing GPU instructions at runtime with OpenCL.
    • Lets say you are working on a CPU without floating point support, then you could emulate it with the JIT without needing a complete code rewrite.
    • At some point in the future it could be possible to offload processing into the GPU or other targets dynamically (I suspect functional languages are somewhat better suited for this).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文