?
Solved

Use of Macros versus Function Calls for speed

Posted on 2003-03-19
6
Medium Priority
?
245 Views
Last Modified: 2010-04-15
hi,
can anyone tell me why using a macro is faster in some cases than a function call.
searching on the internet, the answer found most frequently stated that macros avoid
the "runtime overhead of a function call". Can anyone explain what runtime overhead
this refers to?
thanks!
0
Comment
Question by:rubydiamond
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 225 total points
ID: 8166491
macros are faster in runtime because  macros are inline text replacements


see this
 if u do
#define MAX(a,b) (a>b)?a:b
main(){
printf("%d\n",MAX(3,4));
}

compiler reads the macro and generates the following source code to be compiled further

#define MAX(a,b) (a>b)?a:b
main(){
printf("%d\n",(3>4)?3:4);
}

which is faster then making a function call to the function
int max(int a, int b){
return (a>b)?a:b;
}


making a function call , involves lots of step .. at the runtime ..which makes it slower as compared to macro of same source code.

but if a macro is big enough ( u ca have multi line macros)
and is being used at many places in the code ..
then due to text replacement .. the source codes get quite big and hence the executable generated also gets bigger..

I hope u know , what happens when a basic function call is made at the runtime..

0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8166521
if u are gcc , i can tell u how u can see the preprocessed source code .. ( preprocessing means compiler parses  all the # preprocessor directives and does some of its stuff )

u can see with gcc
gcc -S file.c

( if u want to test this command i wud suggest u to make a very very small program .. with some macro uses .. and no #includes , because compiler replaces the original text of the include file at the place where u do #include )

and u'll observe that all ur macro 'calls' are replaced with appropriate source code .

while function calls will remain as they are.
0
 
LVL 1

Expert Comment

by:frogger1999
ID: 8167045
The above is correct... Macros cause a text replacement at preprocess time so it is like the code is right there and at runtime doesn't even exist.  

Functions have to do a lot of things to be run, most notabley they have to allocate stack space for the parameters and locals that it has, and it has to save off where you where executing before the funtion call.  When it exits it has to deallocate that stack space resotre the program counter etc. This is what the runtime overhead refers to, the stack space and other such things that you must save off to return from the funtion.  

Also if the function is written in a way you may have to do a lot of copying to execute the function

for example

void foo(int x)

has to copy x inorder to call foo.  This can get problematic if instead of an int you pass a structure of ints that is 10kb large or something.

Anyway, these days I don't get too worked up about the runtime overhead of function calls.  Likely if you don't pass large structures by value, don't write functions with a huge amount of local variables (which may have to be used anyway without a new function)  then the cost of calling the function is trival compared to the rest of the program.  

At least in VC++ you do not see the macro expanded and it does make debugging a little trickier.

Hope that helps
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rubydiamond
ID: 8167510
thanks very much - both those answers explain a lot! :)

the application i'm writing has a lot of floating point maths - so i'm trying to convert all this to fixed point maths. I'm working on a processor that isn't very fast at all and so i'm thinking i should be using macros rather than function calls for these fixed point conversions and operations becuase they are used a great deal.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8170305
your decision shud also consider how much calculation do u intend to do in one 'CALL'
also if u have restrictions on memory also .. that also u shud take into consideration.. extensive use of macros may increase ur executable size ..
but i dont think u'll be having that big a program..
0
 
LVL 22

Expert Comment

by:grg99
ID: 8171436
macros are *usually* faster than calls,
EXCEPT in modern computers, where macros may expand your code so much that it won't fit in the level-1 cache (usually 4K to 64K).  If your main loop gets that big, then the CPU will have to get its next instruction from main memory, which may take 5-10 times longer.

Yuor smarter compilers can generate in-line code for intrinsic functions, and some even will inline short functions that you write.   So #defining functions as macros isnt quite a good thing as it used to be.



0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question