Re: When to use macros.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Malcolm McLean

    Re: When to use macros.


    "Malcolm McLean" <regniztar@btin ternet.comwrote in message news:...
    <prashant.khade 1623@gmail.comw rote in message
    >>
    >I know the clear distinction between macro and function.
    >>
    >I know that macro will speed up the program and using function will
    >reduce the size.
    >>
    >But how do we know when to use macro and function. ?
    >>
    In C, use a macro for a trivial function that can take more than one type.
    A
    good example is lerp() - linearly interpolate between a and b by t. It only
    really makes sense if the return value is floating point, but you might want
    float or double. Other examples are min(), max(), and clamp().

    You can also use for trivial functions that read more clearly as macros - I
    often use coin(), for instance, to generate an random number of 0 or 1.
    However generally these are better as functions, because a modern compiler
    will inline anyway.

    Executable size is rarely a consideration. If you've got to reduce
    executable size by replacing macros with functions then you've really
    squeezed your system for every last drop of resource. Speed is also rarely a
    consideration. Only in inner loops will microptimisatio n usually make any
    difference.

    --
    Free games and programming goodies.


Working...