Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How expensive are method calls in .net

Tags:

.net

What is the relative performance cost of calling a method over in-line code?

like image 388
Andrew Avatar asked Mar 17 '10 14:03

Andrew


People also ask

Is calling a function expensive?

A discussion suggested that function calls are expensive and it was even confirmed by referring a post.

Do function calls slow down code?

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.

How expensive are function calls in Javascript?

Cost? About 2.78 microseconds per function call. You heard, me, microseconds.

Do function calls have an overhead?

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.


2 Answers

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.

  • Methods that are greater than 32 bytes of IL
  • Virtual functions
  • Methods that have complex flow control
  • Methods that contain exception-handling blocks
  • If any of the method's formal arguments are structs

Reference: Method Inlining

like image 72
Asad Avatar answered Sep 25 '22 03:09

Asad


The performance cost is so inconsequential as to be irrelevant in comparison to making the code easy to read and its intent clear.

like image 33
Thomas Avatar answered Sep 25 '22 03:09

Thomas