Search code examples
c++performancemethodsinlineoverhead

Will inline speed up void a(string b) { cout << b; }


For an assignment, we're supposed to write two methods for handling outputs. One for outputting strings, and one for integers.

Basically we have two methods calling another method:

void TheClass::displayString(string str){ cout << str; }
void TheClass::displayNumber(int n) { cout << n; }

Will inline speed things up by saving some overhead by not calling yet another function or will it create more in regards to namespaces and such for cout?

inline void TheClass::displayString(string str) { cout << str; }
inline void TheClass::displayNumber(int n) { cout << n; }

Solution

  • Namespaces don't have anything to do with it.

    You probably won't see any benefit here for one simple reason: the function is so small that I'd expect the compiler to be inlining it anyway.

    Remember, the keyword inline is a hint not a directive, and usually you can just let your toolchain decide when to inline. It's good at that.