0

.NET uses the JIT (just-in-time) compilation technique. It means it first compiles the source code into MSIL (Intermediate language) and creates an EXE and later transforms that IL into machine code at runtime. It works fine, but the whole compiled code is not loaded into cache and it compiles all the functions and procedures again and again. My application is very time sensitive. What is happening is that when I call up a particular function very frequently, say through a do while loop, the function gets executed within 10 microseconds. But the same function, if called after an interval of 100 milliseconds or more, it takes 50-60 microseconds to execute. I want some particular functions/procedures to remain in cache all the time. Can anybody guide? I think this is happening because of JIT. Can we turn off JIT and compile the code straight into machine language or something? Please help.

Smi
  • 13,850
  • 9
  • 56
  • 64
Manish Garg
  • 61
  • 2
  • 9
  • Your theory of how the JITer works is quite incorrect. Where did you learn that it works this way? It's leading you down rabbit trails, optimizing the wrong things. No, you can't compile directly into machine language. No, you don't want to turn it off. – Cody Gray - on strike Dec 23 '11 at 10:18

2 Answers2

4

Once function is JITted for the first time, it's never forgotten while the process runs. It's the only first call that might take longer due to IL-to-native compilation. Worst case scenario is that native code might be swapped out if system is short on RAM.

You can skip JIT compilation by running ngen and placing precompiled native image in GAC, although it won't be the solution to your problems. If your code is so time-critical that 50-60 usec makes the difference, you shouldn't use .NET in the first place - not due to JIT, but because of GC which might be triggered virtually at any time by any managed thread.

MagnatLU
  • 5,967
  • 1
  • 22
  • 17
1

No, you can't turn off JIT, because .NET does not "interpret" the IL. It has to be compiled to machine code, and that's done by the JITter.

What you CAN do is pre-compile it. This is done with ngen. see http://msdn.microsoft.com/en-us/magazine/cc163610.aspx

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291