What is the relative performance cost of calling a method over in-line code?
A discussion suggested that function calls are expensive and it was even confirmed by referring a post.
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.
Cost? About 2.78 microseconds per function call. You heard, me, microseconds.
1) Function call overhead doesn't occur. 2) It also saves the overhead of push/pop variables on the stack when function is called. 3) It also saves overhead of a return call from a function. 4) When you inline a function, you may enable compiler to perform context specific optimization on the body of function.
There is a cost
associated with method calls;
Arguments need to be pushed on the stack or stored in registers, the method prolog and epilog need to be executed and so on. The cost of these calls can be avoided by In-lining.
But, JIT
uses a number of heuristics to decide whether a method should be in-lined. Following factors influence JIT, not to In-line a method.
Reference: Method Inlining
The performance cost is so inconsequential as to be irrelevant in comparison to making the code easy to read and its intent clear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With