For example :
Code 1:
void Main()
{
Console.WriteLine("Some texts");
}
Code 2:
void Main()
{
Foo();
}
void Foo()
{
Console.WriteLine("Some texts");
}
Does code 2 run slower than code 1 ? I though when we build the release the JIT will inline code 2 so then code 2 will run as fast as code 1. But when I test them with LinqPad I got the IL result :
Code 1:
IL_0000: ldstr "Some texts"
IL_0005: call System.Console.WriteLine
Code 2:
IL_0000: ldarg.0
IL_0001: call UserQuery.Foo
Foo:
IL_0000: ldstr "Some texts"
IL_0005: call System.Console.WriteLine
IL_000A: ret
As we can see the IL result in code 2 has some extra steps for calling Foo(), does this prove that code 2 run slower than code 1 ?
First off, you’re looking at the IL, not the JITted assembly code. What you have shown doesn’t prove anything. You need to look at the JITted output to see if the JITter inlined the code or not. Note that the JITter differes from platform to platform (x86 vs. x64, for example) and version of the Framework to version of the Framework.
Secondly, of course as written version two will run slower than version one. When I say “as written” I mean that this assumes that the JITter hasn’t inlined the call in version two. The extra call adds a few machine instructions which of course take a few extra cycles to execute (again, don’t forget I said “as written!”). However the difference in performance is highly extremely magnificently unlikely to be meaningful. You would have to be doing this in the tightest of loops for trillions and trillions of iterations to ever see a meaningful performance difference.