By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Speed cost of calling a function vs inline code

P: 93
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. void Swap(int & , int &);
  3.  
  4.  
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?
Mar 15 '07 #1
Share this Question
Share on Google+
12 Replies


100+
P: 1,646
You can declare the function as inline
Mar 15 '07 #2

Expert 100+
P: 1,510
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. void Swap(int & , int &);
  3.  
  4.  
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
Expand|Select|Wrap|Line Numbers
  1. #define swap(type, a, b) {type temp; temp = a; a = b; b = temp;}
  2.  
this may only be called where a statement may be used and the actual parameters must be simple variables
Mar 15 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
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.
Mar 15 '07 #4

dmjpro
100+
P: 2,476
hello horace1 ... ur answer is impressive for coding ....
Mar 15 '07 #5

P: 93
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. elementtype GetElementAt(int x, int y)
  3. {
  4.       return array[x][y];
  5. }
  6.  
  7.  
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. blah blah......
  3.  
  4. timer begin.
  5.  
  6. code
  7.  
  8. get time spent.
  9.  
  10.  
How can I do that?
Mar 15 '07 #6

Ganon11
Expert 2.5K+
P: 3,652
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.
Mar 15 '07 #7

100+
P: 1,646
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
Mar 15 '07 #8

Banfa
Expert Mod 5K+
P: 8,916
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.

Expand|Select|Wrap|Line Numbers
  1. elementtype GetElementAt(int x, int y)
  2. {
  3.       return array[x][y];
  4. }
  5.  
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.
Mar 16 '07 #9

P: 93
Thanx guys.
Mar 17 '07 #10

P: 93
Hi, I discovered this just now.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <windows.h>
  3. #include <mmsystem.h>
  4. #include <iostream.h>
  5.  
  6. class TestClass
  7. {
  8. public:
  9.     int variable;
  10.     int ReturnVariable();                                //Only the declaration.
  11.     int ReturnVariableInline() {return variable;};        //inline.
  12.  
  13.     TestClass();
  14.     ~TestClass();
  15. };
  16.  
  17. int TestClass::ReturnVariable()
  18. {
  19.     return variable;
  20. }
  21.  
  22. TestClass::TestClass()
  23. {
  24.     variable = 0;
  25. }
  26.  
  27. TestClass::~TestClass() {}
  28.  
  29. int main()
  30. {
  31.     TestClass test;
  32.  
  33.     long begin, end;
  34.  
  35.     timeBeginPeriod(1);
  36.  
  37.     int a = 0;
  38.  
  39.     begin = timeGetTime();
  40.  
  41.     for(int i = 0; i < 10000000 ; i++)
  42.     {
  43.         a += test.ReturnVariable();
  44.     }
  45.  
  46.     end = timeGetTime();
  47.  
  48.     cout << "Returning by a function : time :" << end - begin << endl;
  49.  
  50.     begin = timeGetTime();
  51.  
  52.     for(i = 0; i < 10000000 ; i++)
  53.     {
  54.         a += test.ReturnVariableInline();
  55.     }
  56.  
  57.     end = timeGetTime();
  58.  
  59.     cout << "Returning by an inline function : time :" << end - begin << endl;
  60.  
  61.     begin = timeGetTime();
  62.  
  63.     for(i = 0; i < 10000000 ; i++)
  64.     {
  65.         a += test.variable;
  66.     }
  67.  
  68.     end = timeGetTime();
  69.  
  70.     cout << "Simply reading the value : time :" << end - begin << endl;
  71.  
  72.     return 0;
  73. }
  74.  
  75.  

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.
Mar 17 '07 #11

Expert 100+
P: 1,510
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)
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. clock_t  clock_start;                         /* holds clock start time */
  7.  
  8. // start clock
  9. void timer_start() { clock_start = clock(); }
  10.  
  11. // return elapsed time on clock ticks
  12. clock_t timer() {  return((clock() - clock_start) ) ; }
  13.  
  14. class TestClass
  15. {
  16. public:
  17.     int variable;
  18.     int ReturnVariable(int x);                                //Only the declaration.
  19.     inline int ReturnVariableInline(int x) 
  20.        {return variable*x;};        //inline.
  21.  
  22.     TestClass();
  23.     ~TestClass();
  24. };
  25.  
  26. int TestClass::ReturnVariable(int x)
  27. {
  28.     return variable*x;
  29. }
  30.  
  31. TestClass::TestClass()
  32. {
  33.     variable = 5;
  34. }
  35.  
  36. TestClass::~TestClass() {}
  37.  
  38. int main()
  39. {
  40.     TestClass test;
  41.     int z=1;
  42.     long begin, end, i;
  43.  
  44.     timer_start();
  45.     long int a = 0;
  46.     timer_start();
  47.     for(i = 0; i < 1000000000L ; i++)
  48.     {
  49.         a += test.ReturnVariable(z);
  50.     }
  51.     cout << "result " << a << " Returning by a function : time :" << timer() << endl;
  52.  
  53.     a=0;
  54.     timer_start();
  55.     for(i = 0; i < 1000000000L ; i++)
  56.     {
  57.         a += test.ReturnVariableInline(z);
  58.     }
  59.     cout << "result " << a << " Returning by an inline function : time :" << timer()<< endl;
  60.  
  61.     a=0;
  62.     timer_start();
  63.     for(i = 0; i < 1000000000L ; i++)
  64.     {
  65.         a += test.variable*z;
  66.     }
  67.     cout << "result " << a << " Simply reading the value : time :" << timer() << endl;
  68.     system("pause");
  69.     return 0;
  70. }
  71.  
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
Mar 17 '07 #12

100+
P: 1,646
Nice bit of research horace
Mar 17 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.