473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Speed cost of calling a function vs inline code

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
12 6524
willakawill
1,646 1GB
You can declare the function as inline
Mar 15 '07 #2
horace1
1,510 Expert 1GB
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
9,065 Expert Mod 8TB
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
2,476 2GB
hello horace1 ... ur answer is impressive for coding ....
Mar 15 '07 #5
DumRat
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
3,652 Expert 2GB
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
willakawill
1,646 1GB
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
9,065 Expert Mod 8TB
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
DumRat
93
Thanx guys.
Mar 17 '07 #10
DumRat
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
horace1
1,510 Expert 1GB
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
willakawill
1,646 1GB
Nice bit of research horace
Mar 17 '07 #13

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

Similar topics

10
by: Stephan Winter | last post by:
hi, i have a simple question for inline code : class x { private: int xx; public: int getXX(void){ return x; } };
1
by: S Austin | last post by:
Discovered recently (duh) that putting inline code in .h files (e.g. in class definitions) is not a good idea when building DLLs and the applications that use those DLLs. The reason being, of...
16
by: Saroj | last post by:
Is there an way for a function to know who called him ?
2
by: Tom Gao | last post by:
how come something like NavigateURL = 'foo.aspx?cpid=<%# DataBinder.eval( Container.DataItem, "DataItem.Foo"%>&nid=<%# DataBinder.eval( Container.DataItem, "DataItem.Foo2"%>' forgive me if I...
6
by: Anders M | last post by:
I'm trying to use Inline-code to call Page_load, Page_Init or Page_PreRender methods. I've also got a code behind c#-file. I can define inline methods for buttons and so on...that works fine....
8
by: neilmcguigan | last post by:
I just wanted to list some reasons why I prefer inline code to code-behind. 1. you can fix some bugs more quickly. remote desktop into server, change the aspx file, and she's good to go. I'd say...
0
by: kelmen | last post by:
Greeting, I have a weird problem. Working fine before I incoporating theme and skin. my sample is an aspx file using masterpage. working fine. then when I incoporating theme and skin, keep...
5
by: Ajay | last post by:
Hi all, I want to force that compiler take the function INLINE if i declare it inline.I know there is some macro to force the compiler to take the function as inline..but unfortunately i dont know...
1
by: DevNll2002 | last post by:
Can it be done? ( This is a related issue to my recent post... http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/a229a66642bedfaf/870614ae257ae699...
1
by: Sagar | last post by:
Hi, I am working on enhancing an aspx page. The server-side is written with inline code. The coding is not in VS. The task is to make calls a webservice...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.