Is the
inline
keyword still considered good practice?
In C and C++, a function should only be defined in one Translation Unit (roughly equivalent to .c
or .cpp
file). There are only two exceptions:
- functions marked
inline
- class methods defined in the class body (implicitly considered
inline
then)
That is inline
: it means the definition is inline
, and has no bearing at all on whether the function will be inlined at the call site.
Should I trust the infinite wisdom of the compiler?
I understand it's a "joke" but the wisdom of the compiler is only equal to that its developers taught to it. You should in general have a relative degree in confidence in your compiler (you are trusting it with your money, after all) but when it comes to performance at some point you will need to measure if it really matters.
Now, onto the specific function involved:
- yes it is likely to be inlined
- the effect of inlining it will depend on the average length of the
while
loop, the longer the loop runs, the less overhead the function call involves anyway
Oh, and for fun, on the Mill CPU (still being prepared) a function call has about the same cost as a branch (and thus a loop run); therefore, do remember that optimizations are platform dependent, even if you force inlining (with specific attributes) you may well shot down the performance on platforms you did not measure on.