The Gateway to Computer Science Excellence
+2 votes
384 views
Use of macro instead of function is recommended.
(a) when one wants to reduce the execution time
(b) when there is a loop with a function call inside
(c) when a function is called in many places in a program
(d) In (a) and (b) above

Can some one explain how option 'b' is true?
in Programming by Active (1.2k points) | 384 views
0
I think, Ans is not correct

Someone chk plz
0
I think that it may be right.  A function call will take extra space and time as we have to push the function address in the stack and it will take extra time. while if we define the macro the function call will be reduced to a single line or a block of line in the start itself. So It will be fast as compared to defining a new function
0
why not (d). Doesn't it reduce the extra overhead of calling a function and creating an additional activation record?
0
Answer is D but how option b is also true that what i need.

1 Answer

+4 votes
Best answer
If the function call is withing a loop, you're gonna call it for the number of times loop runs. Say, for example, loop runs 1000 times, it will result in time and memory overhead of creating and initializing 1000 activation records in call stack. If you use macro, the function call is replaced by the actual code defined by macro so no its same as executing those lines of code without additional function calling or memory usage.

PS: If function gets inlined -- which normally happens for most small functions, then it has the same effect as macros. Also, there is no guarantee that macros (inlined functions also) will always be faster than functions. This is because they can lead to an increase in code size, which can lead to potential Instruction cache miss. The best place to use macros is where a function has to be executed with different parameters and these parameters are known while writing the code -- this can avoid the execution overhead of processing that parameter.
by Junior (941 points)
selected by
0
i think answer should be A, B, C . because in macros, there is no function call overhead.

.
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,391 answers
198,589 comments
105,442 users