What is the cost of a function call?

后端 未结 15 642
天命终不由人
天命终不由人 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 23:13

    Hard to answer because there are a lot of factors involved.

    First of all, "Simple Memory Access" isn't simple. Since at modern clock speeds, a CPU can add two numbers faster than it get a number from one side of the chip to the other (The speed of light -- It's not just a good idea, it's the LAW)

    So, is the function being called inside the CPU memory cache? Is the memory access you're comparing it too?

    Then we have the function call will clear the CPU instruction pipeline, which will affect speed in a non-deterministic way.

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

    This link comes up a lot in Google. For future reference, I ran a short program in C# on the cost of a function call, and the answer is: "about six times the cost of inline". Below are details, see //Output at the bottom. UPDATE: To better compare apples with apples, I changed Class1.Method to return 'void', as so: public void Method1 () { // return 0; }
    Still, inline is faster by 2x: inline (avg): 610 ms; function call (avg): 1380 ms. So the answer, updated, is "about two times".

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics;

    namespace FunctionCallCost { class Program { static void Main(string[] args) { Debug.WriteLine("stop1"); int iMax = 100000000; //100M DateTime funcCall1 = DateTime.Now; Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < iMax; i++)
            {
                //gives about 5.94 seconds to do a billion loops, 
              // or 0.594 for 100M, about 6 times faster than
                //the method call.
            }
    
            sw.Stop(); 
    
            long iE = sw.ElapsedMilliseconds;
    
            Debug.WriteLine("elapsed time of main function (ms) is: " + iE.ToString());
    
            Debug.WriteLine("stop2");
    
            Class1 myClass1 = new Class1();
            Stopwatch sw2 = Stopwatch.StartNew();
            int dummyI;
            for (int ie = 0; ie < iMax; ie++)
            {
              dummyI =  myClass1.Method1();
            }
            sw2.Stop(); 
    
            long iE2 = sw2.ElapsedMilliseconds;
    
            Debug.WriteLine("elapsed time of helper class function (ms) is: " + iE2.ToString());
    
            Debug.WriteLine("Hi3");
    
    
        }
    }
    

    // Class 1 here using System; using System.Collections.Generic; using System.Linq; using System.Text;

    namespace FunctionCallCost { class Class1 {

        public Class1()
        {
        }
    
        public int Method1 ()
        {
            return 0;
        }
    }
    

    }

    // Output: stop1 elapsed time of main function (ms) is: 595 stop2 elapsed time of helper class function (ms) is: 3780

    stop1 elapsed time of main function (ms) is: 592 stop2 elapsed time of helper class function (ms) is: 4042

    stop1 elapsed time of main function (ms) is: 626 stop2 elapsed time of helper class function (ms) is: 3755

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

    Compared to a simple memory access - slightly more, negligible really.

    Compared to every thing else listed - orders of magnitude less.

    This should hold true for just about any language on any OS.

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