View previous topic :: View next topic |
Author |
Message |
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
Measure the overhead of function call |
Posted: Wed Oct 19, 2005 10:36 am |
|
|
All,
Hi,
I am not a mature programmer in embedded systems so I come up with these questions
Q:
1. How heavy can the overhead of a function call be?
2. Does the overhead vary according to the content and size of the function?
3. Is it possible, in real time systems, function overhead cause significant delay?
4. Are there ways around function overhead?
I come up with these questions because i am trying to modulize my code.
For example, I list some sections of code in a sequential order within main() and now thinking about moving these individual portions of code away into individual functions and then list only the function names in main().
Well, this actually is the way I always adopt but just somehow didn't ever think about the overhead of function call.
Can somebody figure out the puzzle here for me plz? |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Wed Oct 19, 2005 10:47 am |
|
|
Quote: | 1. How heavy can the overhead of a function call be? |
Depends on how many parameters are passed and how many variables are used in the function. Similarly if you call by value as opposed to call by reference.
Quote: |
2. Does the overhead vary according to the content and size of the function?
|
see above
Quote: |
3. Is it possible, in real time systems, function overhead cause significant delay? |
This depends on what you means by significant delay (how long is a piece of string). It takes CPU cycles to copy register values plus the call and return overhead etc.
Quote: |
4. Are there ways around function overhead? |
You can code in-line as you mentioned to avoid calling overhead but it is likely the comiler will introduce calls anyway as part of its optimization.
The best thing to do is compile some code and look at the code produced in the .lst file _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Measure the overhead of function call |
Posted: Wed Oct 19, 2005 11:57 am |
|
|
ye wrote: | All,
Hi,
I am not a mature programmer in embedded systems so I come up with these questions
Q:
1. How heavy can the overhead of a function call be?
2. Does the overhead vary according to the content and size of the function?
3. Is it possible, in real time systems, function overhead cause significant delay?
4. Are there ways around function overhead?
I come up with these questions because i am trying to modulize my code.
For example, I list some sections of code in a sequential order within main() and now thinking about moving these individual portions of code away into individual functions and then list only the function names in main().
Well, this actually is the way I always adopt but just somehow didn't ever think about the overhead of function call.
Can somebody figure out the puzzle here for me plz? |
For a function that is #inline and has no parameters there is no overhead. If parameters are involved add the overhead of copying the variables. For a function that is #seperate add overhead for call and return.
If a function is only called once it will be assembled #inline by default with a few exceptions. If a function is called more than once it will be assembled #seperate by default. Declaring a function #inline or #seperate will override the defaults. |
|
|
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
|
Posted: Wed Oct 19, 2005 4:32 pm |
|
|
Neutone, asmallri, thanks a lot!
All,
I still have some confusions here and may I ask some more questions
Quote: | Depends on how many parameters are passed and how many variables are used in the function |
Quote: | It takes CPU cycles to copy register values plus the call and return overhead etc.
|
I am wondering what this claim is based? Say, how the PIC deal with parameter passings? .... You are saying 'copy register values plus the call and return overhead etc', what exactly does it mean? Are you saying, like, when function A calls function B, function A can't directly access both parameters and variables of function B. Instead, these parameters and variables are copied to some intermediate registers and then used by function A indirectly? |
|
|
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
|
Posted: Wed Oct 19, 2005 4:35 pm |
|
|
Neutone,
I am pretty impressed by your understanding. Wondering if you got those from programming experience or you can recommend any website that gives insightful knowledge? |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Thu Oct 20, 2005 7:41 am |
|
|
You can get a better understanding of all of this by writing some simple code that does these things and then looking at the listing file. In the listing file you can see the assembly generated for these things.
A good example would be a function called twice in a for loop that has parameters. |
|
|
|