Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warm-up when calling methods in C#

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)?

like image 941
Cheng Chen Avatar asked Dec 15 '10 02:12

Cheng Chen


1 Answers

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?

like image 96
BrokenGlass Avatar answered Sep 19 '22 00:09

BrokenGlass