Public Group

# inline functions and macro functions

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

## Recommended Posts

Do small inline functions eg:
inline unsigned char ColourGetA(unsigned Col)
{return (Col & 0xFF000000) >> 24;}


work as well as macro functions in terms of speed since I prefew them with them obeying namespace scope etc? also can the same be said for using contants rather than defines for things like Flag values?

Yes, and yes.

##### Share on other sites
Actually they might even work better. Modern compilers run several passes at different times and different scopes to elimiate constant/invariant expressions before and after inlining, loop unrolling etc.

The whole block of a constant function such as the one you posted can be eliminated early, whereas a macro is simply a text replacement that inserts some "dumb text" which is fed to the parser and which then translates to some statements.
A good compiler will 99% likely detect and eliminate all the individual statements of a constant expression in one of its optimization passes, but it's more efficient and possibly better to discard entire blocks right away. When discarding whole blocks, small-scope optimizations can sometimes work in addition.

Also, a compiler might in theory dedicate only a limited amount of time to the optimizer pass. I am not aware of any real compiler doing this, but there might be such a thing.

##### Share on other sites
Inline functions are usually more efficient actually as the compiler can make the decision *not to* inline certain things, thus saving cache space if the inline code happened to be have hidden complexities or be rarely executed.

Unfortunately compilers aren't perfect and sometimes you'll end up switching to a macro anyway after studying the disassembly. That is compilers have been known to make poor inlining decision (especially when not using profiler-guided optimizations), and if your functions happens to fall just beyond the event horizon and would simplify a long chain of template magic then it can have disastrous consequences. Weirder still is that even when it is inlined the code isn't necessarily seen as equivalent by the compiler, I've had both VC6 and GCC generate worse code without macros.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 13
• 9
• 12
• 9
• ### Forum Statistics

• Total Topics
631438
• Total Posts
3000071
×