Does method method cause performance slowdown?

For instance:
Code 1:

void Main() { Console.WriteLine("Some texts"); } 

Code 2:

 void Main() { Foo(); } void Foo() { Console.WriteLine("Some texts"); } 

Is code 2 running slower than code 1? I though, when we create the release, JIT will be inline code 2, so code 2 will work as fast as code 1. But when I test them with LinqPad I got the result IL:

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 result of IL in code 2 contains several additional steps for calling Foo (), does this prove that code 2 is slower than code 1?

+4
source share
6 answers

First, you are looking at IL, not the JITted assembly code. What you showed does not prove anything. You need to look at the JITted result to see if JITTER has entered the code or not. Note that JITTER differs from platform to platform (for example, x86 or x64) and the Framework version to the Framework version.

Secondly, of course, as it is written, the second version will work more slowly than the first. When I say "as it is written," I mean that it assumes that JITTER did not include the call in the second version. An additional call adds a few machine instructions, which, of course, take a few extra loops to execute (again, don't forget that I said “as it is written!”). However, the performance difference is extremely incredible to be significant. You would have to do this in the most rigorous cycles for trillions and trillions of iterations in order to ever see a significant difference in performance.

+8
source

The method invocation method slows down code execution in a tiny bit if they are not embedded in the C # compiler or jit compiler. However, if your code doesn't run in a loop and runs a million times or so, you should focus on creating clean, understandable, and maintainable code. When I started with programming, the runtime for individual statements was measured in milliseconds or microseconds. Today they are measured in nanoseconds. Usually time is spent mainly on I / O operations. Bad algorithms can also be blamed several times. If your design is clearly structured, it is much easier to replace the poorly executed part of the code with a better one, compared to the time-optimized code from the very beginning and, therefore, probably poorly structured.

I survived this recently. I had to create complex graphics in Visio in a C # program. It turned out that Visio automation was very slow. It took several minutes to create the graphics. Fortunately, I placed all the graphics components in a component that opened graphics commands through the neutral interface of the product. Ie: The interface did not contain specific Visio materials. It was very easy to replace the Visio component with a new SVG component that performed the same task in less than a second. In addition, absolutely no changes should have been made in my algorithms or in any other part of my program.

Because of this, my graphical component adds more method calls. In addition, it can be accessed through the interface, which further slows down the work. However, in the end, it was this interface and these additional method calls that allowed me to implement a much faster solution. Remember: minutes versus less than one second!

+2
source

In this case, with the implementation, since this was the last time I read about such things, yes, it will be nested.

In general, the answer may be - what kind of documentation there is according to the rules for inlaying - this is informative material, mainly on blogs, and not normative documentation. Not only will the details change between versions, they will almost certainly be.

On practice:

If you suspect that a hot spot of performance might benefit from manual insertion, try profile again and again. Or at least look at the jitted code for this particular part.

In theory:

However, I like to know that small methods are embedded, anyway.

+2
source

Its so trivial (read: maybe), you should not worry about it.

+1
source

The C # compiler does not make attachments, and this is what you see in the IL code. Inlining is the work of the JIT optimizer, which does this at run time (if it decides to embed a function, makes the program more efficient).

+1
source

The compiler creates its own interpretation of the code (assembly code), so the code examples will be processed in the same processor. (in release mode)

0
source

Source: https://habr.com/ru/post/1385144/


All Articles