What is the cost of a function call?

后端 未结 15 640
天命终不由人
天命终不由人 2020-12-12 22:34

Compared to

  • Simple memory access
  • Disk access
  • Memory access on another computer(on the same network)
  • Disk access on another computer
相关标签:
15条回答
  • 2020-12-12 22:52

    A function call is simply a shift of the frame pointer in memory onto the stack and addition of a new frame on top of that. The function parameters are shifted into local registers for use and the stack pointer is advanced to the new top of the stack for execution of the function.

    In comparison with time

    Function call ~ simple memory access
    Function call < Disk Access
    Function call < memory access on another computer
    Function call < disk access on another computer

    0 讨论(0)
  • 2020-12-12 22:53

    Let's not forget that C++ has virtual calls (significantly more expensive, about x10) and on WIndows you can expect VS to inline calls (0 cost by definition, as there is no call left in the binary)

    0 讨论(0)
  • 2020-12-12 22:56

    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
    0 讨论(0)
  • 2020-12-12 22:56

    Depends on what that function does, it would fall 2nd on your list if it were doing logic with objects in memory. Further down the list if it included disk/network access.

    0 讨论(0)
  • 2020-12-12 23:04

    Assuming you mean the overhead of the call itself, rather than what the callee might do, it's definitely far, far quicker than all but the "simple" memory access.

    It's probably slower than the memory access, but note that since the compiler can do inlining, function call overhead is sometimes zero. Even if not, it's at least possible on some architectures that some calls to code already in the instruction cache could be quicker than accessing main (uncached) memory. It depends how many registers need to be spilled to stack before making the call, and that sort of thing. Consult your compiler and calling convention documentation, although you're unlikely to be able to figure it out faster than disassembling the code emitted.

    Also note that "simple" memory access sometimes isn't - if the OS has to bring the page in from disk then you've got a long wait on your hands. The same would be true if you jump into code currently paged out on disk.

    If the underlying question is "when should I optimise my code to minimise the total number of function calls made?", then the answer is "very close to never".

    0 讨论(0)
  • 2020-12-12 23:05

    The cost of actually calling the function, but not executing it in full? or the cost of actually executing the function? simply setting up a function call is not a costly operation (update the PC?). but obviously the cost of a function executing in full depends on what the function is doing.

    0 讨论(0)
提交回复
热议问题