问题
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 ?
回答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.
回答2:
Yes method calls slow down the code execution a tiny little bit, if they a not inlined by the c#-compiler or the jit-compiler. However, unless your code runs in a loop and is executed a million times or so, you should really focus on producing clean, understandable and maintainable code. When I started with programming the execution times for single statements where measured in milli- or microseconds. Today they are measured in nanoseconds. Time usually is mainly wasted for I/O operations. Bad algorithms can also be blamed some times. If your design is cleanly structured, it will be much easier to replace a poorly performing code-part by a better one, compared to a code that was time-optimized from the beginning and therefore probably badly structured.
I experienced that recently. I had to produce a complicated graphic in Visio in a c# program. It turned out that Visio-automation was very slow. It took minutes to create the graphic. Fortunately, I had put all the graphics stuff in a component, which exposed graphics commands through a product neutral interface. I.e.: the interface did not contain any Visio specific stuff. It was very easy to replace my Visio component by a new SVG component, which did the same task in less than a second. In addition, absolutely no changes had to be made in my algorithms or in any other part of my program.
Of cause, my graphics wrapper component adds more method calls. In addition, it is accessed via an interface, which slows down the whole thing even more. However, in the end, it was this interface and these extra method calls, which allowed me to implement a much faster solution. Remember: minutes versus less than one second!
回答3:
In this case, with the implementation as it was the last time I read on such things, yes it will be inlined.
In general, the answer is maybe - what documentation is out there on the rules for inlining is informative material, mostly in blogs, rather than normative documentation. Not only could the details change between versions, they almost certainly will.
In practice:
If you suspect that a performance hot-spot could benefit from manual inlining, then try it and profile again. Or at least look at the jitted code for that particular piece.
In theory:
Still, I like knowing that small methods are inlined, all the same.
回答4:
Its so trivial (read: there might be), you shouldn't need to worry about it.
回答5:
The C# compiler does not do inlining, and this is what you see in the IL code. Inlining is the job of the JIT optimizer, which does it during runtime (if it decides inlining a function makes the program more efficient).
回答6:
The compiler creates his own interpretation of the code (assembly code), so the code examples will be processed in the same thing for the CPU. (in release mode)
来源:https://stackoverflow.com/questions/8418045/does-call-method-slow-down-performance