Reputation: 80378
We have a C# program that performs some timing measurements. Unfortunately, the first time it measures is 20x slower than the second and subsequent times, probably due to JIT (Just-In-Time) compiling.
Is there a way to ensure, on startup, that every line of MSIL code in the entire application is compiled with the JIT compiler?
Upvotes: 4
Views: 2789
Reputation: 391664
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.
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.
ie. something like this:
MethodUnderTest();
Stopwatch sw = Stopwatch.StartNew();
for (int index = 0; index < 1000; index++)
MethodUnderTest();
sw.Stop();
Upvotes: 1
Reputation: 17909
As ta.speot.is said, the answer is probably using 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:
http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.71).aspx
Upvotes: 6