Jump to content
  • Advertisement
Sign in to follow this  
staticVoid2

faster using macros

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, I was just wondering if it would be faster to use macros for basic calculations like vector addition, dot, cross products etc. than to use standard functions, I've seen this done in the quake source code but what are the benefits?

Share this post


Link to post
Share on other sites
Advertisement
Using a macro for this has the same benefits and caveats as using one for everything. The code is copied every time it's used so there is no function call overhead, but there is a size overhead for the same reason.

If you're using this very often (inside a loop for example) then I'd probably use an inline function or macro because the call overhead in that case may be significant, but if the loop isn't unrolled (which it may not be) then the size overhead will be insignificant.

Share this post


Link to post
Share on other sites
A macro is just a piece of text with placeholders in it, which, when the macros are expanded, are replaced with the text you pass to the macro. An inline function is a piece of code that is inserted at the point of invocation of the function, thus no function call overhead.

You don't use macros for this case anymore because the inline keyword is supported in modern compilers. It's just safer. (E.g. when you define a macro #define square(x) ((x)*(x)) and you invoke it with int bla=square(i++) then the expanded code would be int bla=((i++)*(i++)), which is obviously not what you want. An inline function does what is expected and is just as fast.)

Also note that function calls on CPUs like Core etc. are so cheap that function inlining may actually hurt performance because the larger code might not fit anymore into the instruction cache.

Share this post


Link to post
Share on other sites
Quote:
Original post by staticVoid2
so whats the difference between macro and inline?


(Assuming C++)
Using macros you force the inlining, lose features like namespaces and type checking, and get the possibility of hard to spot bugs because arguments are literally copied. In general use the inline keyword, then the compiler can decide whether it will improve performance at all to inline it.

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
Quote:
Original post by staticVoid2
so whats the difference between macro and inline?


(Assuming C++)
Using macros you force the inlining, lose features like namespaces and type checking, and get the possibility of hard to spot bugs because arguments are literally copied. In general use the inline keyword then the compiler can decide whether it will improve performance at all to inline it.


I believe that in recent MVC and gcc versions, inline has no practical impact. Compilers will inline anything they perceive will improve performance (if requested). Even worse, MVC 2008 has some such aggressive inline analysis, that it's capable of deducing certain simple algorithms, and performing them during compile-time. For example, summing up a set of compile-time constants will result in calculation performed by compiler. For this reason it's preferable to use functions, since macros may obscure the algorithm, and confuse the compiler, resulting in less optimized code.

On modern compilers for modern applications, macros offer no performance benefit whatsoever, at expense of huge loss of code clarity.

There are certain situations where macros may improve performance, but it's unrelated to code, and more to certain code organizational aspects. But those apply only to some really specific and detailed aspects. This is related mostly to certain value access across compilation units, and this type of problem can be solved with templates, allowing additional optimization to be performed.

Use macros if you *need* copy-pasted code, and literally copy-pasted code. For example, function calling convention and other parameters, long names and namespaces for automatically managed classes and functions, or perhaps various forms of meta information (RPC, serialization, ORM), but only if that code is handled in (almost)completely automated fashion. (almost)Never use it for actual logic.

Share this post


Link to post
Share on other sites

#define sign(a)(a < 0 ? -1 : 1)

inline int sign(a)
{
if(a < 0) return -1;
else return 1;
}




so, in this case, which one would be better(more efficient, faster)?

Share this post


Link to post
Share on other sites
The function is better in all ways.

EDIT: And although it is entirely irrelevant to the generated code with almost 100% certainty, you've made the function "look" more complex, perhaps from an unconscious bias toward the macro. However, since macros are simply text substitutions, you may write the sign function as:

inline int sign( int a )
{
return a < 0 ? -1 : 1;
}


Although for more complex operations I'd prefer your original if/else for readability, most likely.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!