Why subsequent direct method call is much faster than the first call?

This is because of the Just In Time (JIT) compilation method that is used for .NET apps. The MSIL bytecode is translated to machine code once by the JIT compiler and subsequent executions of that code are much faster because the native version has been generated and cached.

You pay a one time penalty when you run your code, but the JIT compiler can also perform optimizations for the current architecture that it could not be performed if the code were native from the get-go. You can however force a JIT pass by calling RuntimeHelpers.PrepareMethod.

Leave a Comment