10

I just came across this post that talks about time measuring. I remember (I hope I'm not misremembering) it's an unfair competition, if this method is never called before. That is:

// At the beginning of the application
MyClass instance = new MyClass();
instance.MyMethod();
instance.MyMethod();  // Faster than the first call, because now it's warmed up.

Do we really have such warming-up theory in C#? If yes, why (what will the CLR do when warming-up)? And is everything the same if this method is an extension one (a static one)?

Community
  • 1
  • 1
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • For what it's worth, I feel like Objective-C does the same thing although it has nothing to do with a JIT/ – Chris Thompson Dec 15 '10 at 02:40
  • I believe that Windows does some level of executable caching as well; the first time you call a function it'll take longer, period. If you're really doing measurements, it's a good idea to get a large sample size. You can further mitigate against things like first-time loading by discarding any outliers from your sample set. – overslacked Dec 15 '10 at 03:12
  • @overslacked yeah depending on which windows version you have different ways of caching for applications, but I doubt this affects execution speed of a method though. The method would be called after the application is loaded in memory. –  Dec 15 '10 at 04:21
  • @Stefan - I don't recall all of the specifics, but from what I do recall the entire executable image is not loaded initially - the functions are loaded as they're used. So there would be a delay when calling a function the first time - this "warm-up" is not at all unique to JITted applications, which is why proved measurement methods should be used (although the JIT overhead certainly exacerbates the problem). My main point is regarding the methods, not the specifics of Windows exe caching which, I'm sure you're right, is much different between Windows versions. – overslacked Dec 15 '10 at 06:11

4 Answers4

7

If by "warm up" you refer to JIT'ing then yes - if a method is never called it won't have been compiled yet, so the very first time you run it it might be slower. Also refer to Does the .NET CLR JIT compile every method, every time?

Community
  • 1
  • 1
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
6

This is due to just-in-time (JIT) compilation. If you want to improve performance and avoid this effect, the Native Image Generator (Ngen.exe) can help you.

Andy West
  • 12,302
  • 4
  • 34
  • 52
4

What people are talking about here is just-in-time compilation. The code you create in .NET is stored in intermediate language, which is platform independent. When you are running the application parts of the CIL code are compiled to platform-specific instructions which takes a bit of time the first time around. Then it gets cached so the next time you call the method you don't have this time loss.

If you really want to, you can pre-compile to platform specific versions though.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

It needs to be compiled, and that is why the first call is longer.

From Compiling MSIL to Native Code:

On the initial call to the method, the stub passes control to the JIT compiler, which converts the MSIL for that method into native code and modifies the stub to direct execution to the location of the native code. Subsequent calls of the JIT-compiled method proceed directly to the native code that was previously generated, reducing the time it takes to JIT-compile and run the code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
S.Skov
  • 707
  • 4
  • 8