Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cost of a function call?

Compared to

  • Simple memory access
  • Disk access
  • Memory access on another computer(on the same network)
  • Disk access on another computer(on the same network)

in C++ on windows.

like image 909
FlinkmanSV Avatar asked Sep 18 '08 17:09

FlinkmanSV


People also ask

Are function calls expensive?

Speaking from personal experience, I write code in a proprietary language that is fairly modern in terms of capability, but function calls are ridiculously expensive, to the point where even typical for loops have to be optimized for speed: for(Integer index = 0, size = someList.

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.

What is a function call?

A function call is an expression containing the function name followed by the function call operator, () . If the function has been defined to receive parameters, the values that are to be sent into the function are listed inside the parentheses of the function call operator.


1 Answers

relative timings (shouldn't be off by more than a factor of 100 ;-)

  • memory-access in cache = 1
  • function call/return in cache = 2
  • memory-access out of cache = 10 .. 300
  • disk access = 1000 .. 1e8 (amortized depends upon the number of bytes transferred)
    • depending mostly upon seek times
    • the transfer itself can be pretty fast
    • involves at least a few thousand ops, since the user/system threshold must be crossed at least twice; an I/O request must be scheduled, the result must be written back; possibly buffers are allocated...
  • network calls = 1000 .. 1e9 (amortized depends upon the number of bytes transferred)
    • same argument as with disk i/o
    • the raw transfer speed can be quite high, but some process on the other computer must do the actual work
like image 75
mfx Avatar answered Oct 09 '22 01:10

mfx