Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does call method slow down performance?

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 ?

like image 316
JatSing Avatar asked Dec 07 '11 15:12

JatSing


People also ask

Does calling a function takes time Python?

Let's begin by making the following observation: function calls in Python are not very fast. Don't get me wrong, this doesn't mean that you should not use functions at all, quite the contrary. The point is that function calls alone can take non-trivial amount of time and that you should be aware of this.

Do using functions make code run faster?

Functions CAN make code faster by coding logic once instead of repeating several times and thus reducing code size and resulting in better CPU cache usage. Functions CAN make code slower by copying parameters and hiding info from the optimization.

Are functions slow in C++?

Performance has always been a high priority for C++, yet there are many examples both in the language and the standard library where compilers produce code that is significantly slower than what a machine is capable of.

What is call in method?

The call() allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method. With call() , you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.


1 Answers

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.

like image 198
jason Avatar answered Nov 07 '22 13:11

jason