简体   繁体   English

在C#中,在启动时,有没有办法强制JIT编译器触摸整个应用程序中的所有MSIL代码?

[英]In C#, on startup, is there a way to force the JIT compiler to touch all MSIL code in the entire app?

We have a C# program that performs some timing measurements. 我们有一个C#程序执行一些定时测量。 Unfortunately, the first time it measures is 20x slower than the second and subsequent times, probably due to JIT (Just-In-Time) compiling. 不幸的是,它第一次测量比第二次和随后的时间慢20倍,可能是由于JIT(即时)编译。

Is there a way to ensure, on startup, that every line of MSIL code in the entire application is compiled with the JIT compiler? 有没有办法确保在启动时整个应用程序中的每一行MSIL代码都是用JIT编译器编译的?

As ta.speot.is said, the answer is probably using NGEN; 正如ta.speot.is所说,答案可能是使用NGEN; this is a tool the pre-jits your assembly and turns it into native code for the platform you're running on. 这是一个预装配件并将其转换为您运行的平台的本机代码的工具。

Often its run during the setup phase of your application because of this. 因此,它通常会在应用程序的设置阶段运行。

MSDN have a guide here: MSDN有一个指南:

http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.71).aspx http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.71).aspx

If you can't use NGEN, as suggested by @ta.speot.is in the comment, or by @Russ C in his answer , a different way is to "warm up" the JITter for the code involved. 如果您不能使用NGEN,如@ ta.speot.is在评论中所建议的那样 ,或者在答案中使用 @Russ C ,则另一种方法是为所涉及的代码“热身”JITter。

Basically, if you call some code 1000s of times in order to do performance measurements, you call it once without timing it, just to JIT all the code, before you do the actual measurements. 基本上,如果为了进行性能测量而调用一些代码1000次,那么在进行实际测量之前,只需将其调用一次,而不是对所有代码进行JIT。

ie. 即。 something like this: 这样的事情:

MethodUnderTest();

Stopwatch sw = Stopwatch.StartNew();
for (int index = 0; index < 1000; index++)
    MethodUnderTest();
sw.Stop();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM