将.NET代码预编译为机器代码 [英] Precompile .NET code into machine code

查看:135
本文介绍了将.NET代码预编译为机器代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我正在创建一个应用程序,其中包含许多需要
的复杂算法 几乎有时间执行...
我知道由于CLR,C#代码(MSIL)被转换为机器代码. 但是如何手动将关键的和复杂的方法预编译为机器代码
立刻...

请参阅有关互斥锁黑客的文章:
Hack Mutex(阻止其他程序)

Hi guys!
I''m creating an application which has a bunch of complex algorithms that require
pretty much time to execute ...
I know that C# code (MSIL) is converted to machine code due to CLR..
but how can I manually precompile my critical and complex methods into machine code
at once...

See my artice about mutex hack:
Hack Mutex (blocking other programs)

推荐答案

看看NGEN 工具 [ ^ ].
Have a look at the NGEN tool[^].


如果您确定是算法使速度放慢了(您会惊讶地发现它经常是完全其他的东西),那么最好一种操作是将它们重新编码为本地语言的DLL,然后从您的C#中调用它.

首先对其进行分析,并检查您是否确实知道需要花费时间.然后专注于该位.
If you are sure it is the algorithms that are slowing things down (and you would be surprised how often it turns out to be something else entirely) then the best course of action is to recode them into a DLL in a native language, and call that from your C#.

Profile it first, and check that you really know what is taking time. Then focus on that bit.


NGEN是用于生成机器代码的工具.但是,在某些情况下,与使用JIT相比,NGEN可能导致代码优化程度降低.主要是因为JIT可以利用处理器模型特定的指令,而NGEN为一系列处理器创建代码.另外,NGEN使部署过程复杂化,因为您必须将NGEN映像部署到特殊位置,而不仅仅是在本地复制您的exe和dll. NGEN的大多数好处都在于启动时间,因为每种方法的JIT惩罚只会发生一次.

最重要的是:使用JIT和NGEN对应用程序进行概要分析,并查看哪个选项是最佳选择.

一些有用的链接:

http://msdn.microsoft.com/en-us/magazine/cc163610.aspx [ ^ ]
http://msdn.microsoft.com/en-us/magazine/cc163808.aspx#S2 [ ^ ]
NGEN is the tool for generating machine code. However, in some cases NGEN can cause code to be less optimized than if you used JIT. Mainly because JIT can take advantage of processor model specific instructions, whereas NGEN creates code for a family of processors. Also, NGEN complicates the deployment process because you have to deploy the NGEN images to a special location instead of just copying your exe''s and dll''s locally. Most of the benefits of NGEN are in startup time because the JIT penalty for each method only happens once.

The bottom line: Profile your application using JIT and NGEN and see which option is best.

Some useful links:

http://msdn.microsoft.com/en-us/magazine/cc163610.aspx[^]
http://msdn.microsoft.com/en-us/magazine/cc163808.aspx#S2[^]


这篇关于将.NET代码预编译为机器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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