.NET - Using Just In Time (JIT) Compiling
.NET programs are not compiled directly into executable code but are compiled into an intermediary language known as Microsoft Intermediary Language (MSIL or IL). This language is compiled and executed later. You may have heard of JIT compiling or "the JITer" from Java, but the similarity between .NET and Java in merely in the name.
To better understand what JITing in .NET is all about, first consider a solution that does not use .NET. If you develop a solution using Visual C++, the output of the compilation process is machine code that is native to the targeted processor/system and is directly executable. A traditional Windows program, for example, would have an .EXE file that you can double-click to make it run.
In .NET things are a little different, because .NET aims for optimization and the ability to run on any device, the compiler emits the program in an intermediary language known as Microsoft intermediary language (MSIL or IL). Essentially, this is a processor-agnostic program that cannot run as is. When you run the program, the common language runtime loads the code into the runtime environment and a just-in-time compiler (JITer) compiles the IL into native executable code. This native code is then executed by the runtime's execution engine.
Benefits of JIT Compiling
The most obvious benefit of the common language runtime's JIT scheme is that it takes system-agnostic IL code and compiles it into native code for the system upon which it is running. This means that if you take a .NET program and run it on a four-way symmetric Itanium system, the runtime will optimize the code for that 64-bit processor. ON the other hand, if you run that same code on an older Pentium II 300 MHz-based system, .NET will tailor that same IL code to the Pentium II's instruction set!
Like the garbage collector, the just-in-time process is optimized to make code run as efficiently as possible. It compiles only when it needs to execute the code. The just-in-time compiler also caches compiled code to reduce the need to continuously recompile the same code every time it is called.
Note: It is possible for you to pre-just-in-time compile your entire .NET application. The advantage, however, is arguable because, although your program will initially execute faster, this speed advantage wanes over time. Recall that normally the just-in-time compiler caches code it compiles. Once the compiler has "touched" the more frequently executed pieces of the solution no additional compilation is necessary and the advantage of pre-just-in-time compilation becomes negligible.
How is it Different From Java's JVM?
An important distinction between how .NET executes code and Java's use of a JITer is worth mentioning. On the java platform, a just-in-time compiler is used to transform the Java program into platform-independent code known as byte code. In great contrast to .NET, byte code is not an intermediary language that is compiled to native, hardware-optimized code, but is instead, interpreted at execution time by the Java virtual machine. This process of interpreting code as it executes is much slower than the compile-and-cache method used by the .NET JITer.