-2

Possible Duplicate:
Inline functions vs Preprocessor macros
what is concept of Inline function and how it is differ from macro?

inline unsigned int getminutes( unsigned int seconds ) { return( seconds / 60 ); } #define GetMinutes(seconds) (seconds) / (60) 

To be honest I'd ask which one is faster, but I've seen so much on S.O that asking which one is better would grant me me knowledge. (Yes! I'm a knowledge hunter)

5

4 Answers 4

4

Never use a macro if you can use an inline function to achieve the same. The compiler is going to generate exactly the same code for both of the solutions you provided, assuming you are using a fairly decent one.

Of course there is no guarantee that inline functions will actually be inlined, but in these cases, if your compiler can't inline that function, then it's probably a really bad one.

Just don't use macros unless you really need to(header guards, do repetitive stuff, etc). Macros are evil in several ways, you can read a lot about that if you search for information online.

Sign up to request clarification or add additional context in comments.

Comments

3

I guess the macro will be faster if you consider that inline is not guaranteed by the compiler to be used. If the function is not inlined, then you have the overhead of a function call.

The macro will be expanded in place by the preprocessor, so it's always going to be inline. The macro is also not type safe and has global scope.

Functions are preferred.

1 Comment

If the compiler is so bad that it cannot inline a one-liner, how do we know that it doesn't implement the divide as a call to RTL_incredibly_slow_divide(seconds, 60)? Then the macro will effectively not be inlined either.
1

Unfortunately, which is faster is one of those cases where the only way to know is to profile. I suspect, however, that the result is the same in typical release build settings.

Which is better, however, I'd say the inline function. Easier to debug. Safer than a macro.

I avoid macros except where absolutely necessary. I think of them as compile-time find-and-replace. I consider find-and-replace to be extremely dangerous at worst. I actually wrote a post or two about why I dislike #define macros so intensely...

Another word of advice I run on: The compiler knows better than you. The macro will force inline, even if it's actually not good for performance. inline will suggest it as a candidate for inlining, but may not inline if it doesn't meet criteria to be inlined.

Comments

0

With a good optimizing compiler the performance will be identical. The difference is that the inline function is more or less a suggestion to the compiler. Although the compiler should in most cases honor the suggestion, the macro version will force the compiler to inline the code.

As an aside, your macro should be written ((seconds) / 60) to make sure the intended grouping is used in all cases.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.