Speed cost of calling a function vs inline code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DumRat
    New Member
    • Mar 2007
    • 93

    #1

    Speed cost of calling a function vs inline code

    Hi, I want to know something about functions.

    Say I need to swap two integers frequently in my program.
    The ususal way will be to declare

    Code:
    void Swap(int & , int &);
    But I am sure that using a function is slower than just typing the code where I want. I want to know how much a function call costs. by how much will it slow the program?
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    You can declare the function as inline

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      Originally posted by DumRat
      Hi, I want to know something about functions.

      Say I need to swap two integers frequently in my program.
      The ususal way will be to declare

      Code:
      void Swap(int & , int &);
      But I am sure that using a function is slower than just typing the code where I want. I want to know how much a function call costs. by how much will it slow the program?
      you can also use a preprocessor macro
      Code:
      #define swap(type, a, b) {type temp; temp = a; a = b; b = temp;}
      this may only be called where a statement may be used and the actual parameters must be simple variables

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by DumRat
        But I am sure that using a function is slower than just typing the code where I want. I want to know how much a function call costs. by how much will it slow the program?
        It is hard to tell without measuring it for your specific system, however it can be significant, especially for a small function call a lot of times.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          hello horace1 ... ur answer is impressive for coding ....

          Comment

          • DumRat
            New Member
            • Mar 2007
            • 93

            #6
            1.Ok. Here is my problem. I've got a 200 * 200 array of elements. And I have made the array a private member of a class. I use the class to handle the array, so, if I wanted to get an element of the array from outside the class, I would have to call a method. The method is, simple.

            Code:
            elementtype GetElementAt(int x, int y)
            {
                  return array[x][y];
            }
            I am using the function on frequent terms. I am copying the array, and doing some more things with it, so I am using the above method too much.

            By how much will the performance increase if I were to make the array public?

            2. How can I get the time spent for a piece of code?
            ex.

            Code:
            blah blah......
            
            timer begin.
            
            code
            
            get time spent.
            How can I do that?

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              You should be able to use time time() function, but any single call will take less time than this can measure (I think). You might consider measuring the time a portion of code takes that uses this GetElementAt repeatedly, and compare times between the function and directly accessing the array.

              Comment

              • willakawill
                Top Contributor
                • Oct 2006
                • 1646

                #8
                The best way I know, and I do this in my apps, is to put the code into a loop and measure how long it takes to process, say, a million iterations. When you make small adjustments to the process it will alter the time considerably

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by DumRat
                  I use the class to handle the array, so, if I wanted to get an element of the array from outside the class, I would have to call a method. The method is, simple.

                  Code:
                  elementtype GetElementAt(int x, int y)
                  {
                        return array[x][y];
                  }
                  I am using the function on frequent terms. I am copying the array, and doing some more things with it, so I am using the above method too much.

                  By how much will the performance increase if I were to make the array public?
                  By about the same amount as if you just defined the function (as opposed to only declaring it) in the class definition (in the header file) so that the function is inlined. In fact this is an excelent example of a function to inline, it doesn't really do anything so inlining it will remove the overhead of the function call (and thus most of the overhead of the code).

                  Define put all my class methods that just return the value of private members in the class definition to reduce this overhead.

                  Comment

                  • DumRat
                    New Member
                    • Mar 2007
                    • 93

                    #10
                    Thanx guys.

                    Comment

                    • DumRat
                      New Member
                      • Mar 2007
                      • 93

                      #11
                      Hi, I discovered this just now.

                      Code:
                      #include <windows.h>
                      #include <mmsystem.h>
                      #include <iostream.h>
                      
                      class TestClass
                      {
                      public:
                      	int variable;
                      	int ReturnVariable();								//Only the declaration.
                      	int ReturnVariableInline() {return variable;};		//inline.
                      
                      	TestClass();
                      	~TestClass();
                      };
                      
                      int TestClass::ReturnVariable()
                      {
                      	return variable;
                      }
                      
                      TestClass::TestClass()
                      {
                      	variable = 0;
                      }
                      
                      TestClass::~TestClass() {}
                      
                      int main()
                      {
                      	TestClass test;
                      
                      	long begin, end;
                      
                      	timeBeginPeriod(1);
                      
                      	int a = 0;
                      
                      	begin = timeGetTime();
                      
                      	for(int i = 0; i < 10000000 ; i++)
                      	{
                      		a += test.ReturnVariable();
                      	}
                      
                      	end = timeGetTime();
                      
                      	cout << "Returning by a function : time :" << end - begin << endl;
                      
                      	begin = timeGetTime();
                      
                      	for(i = 0; i < 10000000 ; i++)
                      	{
                      		a += test.ReturnVariableInline();
                      	}
                      
                      	end = timeGetTime();
                      
                      	cout << "Returning by an inline function : time :" << end - begin << endl;
                      
                      	begin = timeGetTime();
                      
                      	for(i = 0; i < 10000000 ; i++)
                      	{
                      		a += test.variable;
                      	}
                      
                      	end = timeGetTime();
                      
                      	cout << "Simply reading the value : time :" << end - begin << endl;
                      
                      	return 0;
                      }

                      the output on my machine is like this :

                      373
                      338
                      23

                      So, an inline function is faster than an ordinary one. But just reading the value is way faster. Anyway, thanx for the help offered.

                      Comment

                      • horace1
                        Recognized Expert Top Contributor
                        • Nov 2006
                        • 1510

                        #12
                        the results can depend on the compiler and level of optimization used.

                        this is a similar program (which uses functions from <time.h> to measure time)
                        Code:
                        #include <iostream.h>
                        #include <time.h>
                        #include <stdlib.h>
                        
                        
                        clock_t  clock_start;                         /* holds clock start time */
                        
                        // start clock
                        void timer_start() { clock_start = clock(); }
                        
                        // return elapsed time on clock ticks
                        clock_t timer() {  return((clock() - clock_start) ) ; }
                        
                        class TestClass
                        {
                        public:
                        	int variable;
                        	int ReturnVariable(int x);								//Only the declaration.
                        	inline int ReturnVariableInline(int x) 
                               {return variable*x;};		//inline.
                        
                        	TestClass();
                        	~TestClass();
                        };
                        
                        int TestClass::ReturnVariable(int x)
                        {
                        	return variable*x;
                        }
                        
                        TestClass::TestClass()
                        {
                        	variable = 5;
                        }
                        
                        TestClass::~TestClass() {}
                        
                        int main()
                        {
                        	TestClass test;
                            int z=1;
                        	long begin, end, i;
                        
                        	timer_start();
                        	long int a = 0;
                        	timer_start();
                        	for(i = 0; i < 1000000000L ; i++)
                        	{
                        		a += test.ReturnVariable(z);
                        	}
                        	cout << "result " << a << " Returning by a function : time :" << timer() << endl;
                        
                            a=0;
                        	timer_start();
                        	for(i = 0; i < 1000000000L ; i++)
                        	{
                        		a += test.ReturnVariableInline(z);
                        	}
                        	cout << "result " << a << " Returning by an inline function : time :" << timer()<< endl;
                        	
                            a=0;
                        	timer_start();
                        	for(i = 0; i < 1000000000L ; i++)
                        	{
                        		a += test.variable*z;
                        	}
                        	cout << "result " << a << " Simply reading the value : time :" << timer() << endl;
                            system("pause");
                        	return 0;
                        }
                        with g++ no optimization
                        result 705032704 Returning by a function : time :10641
                        result 705032704 Returning by an inline function : time :10281
                        result 705032704 Simply reading the value : time :5031

                        with g++ optimization -O3
                        result 705032704 Returning by a function : time :875
                        result 705032704 Returning by an inline function : time :860
                        result 705032704 Simply reading the value : time :875

                        with Turbo C V3.0 small memory model
                        result 705032704 Returning by a function : time :515
                        result 705032704 Returning by an inline function : time :330
                        result 705032704 Simply reading the value : time :330

                        with Turbo C V3.0 huge memory model
                        result 705032704 Returning by a function : time :2359
                        result 705032704 Returning by an inline function : time :337
                        result 705032704 Simply reading the value : time :338

                        Comment

                        • willakawill
                          Top Contributor
                          • Oct 2006
                          • 1646

                          #13
                          Nice bit of research horace

                          Comment

                          Working...