5

I have a class cnVector that represents a point in 3 dimensional space. Its operators + - * / are used intensively.
Their implementation is very short:

cnVector cnVector::operator + (const cnVector& v) const { return cnVector( x + v.x, y + v.y, z + v.z ); } 

My question is, because this function is very short, should I inline it although its intensive use? or would it generate too much code when using it that much?

4
  • 1
    I mark functions this simple as inline. It doesn't matter much though, if you tell the compiler to inline any that look good. "Inline any suitable" on MSVC. Dunno about GCC. Commented Oct 23, 2011 at 17:58
  • 2
    GCC passing -finline-functions will instruct it to automatically inline functions it thinks should be inlined. Commented Oct 23, 2011 at 18:01
  • 2
    My GCC (4.4.5) applies -finline-small-functions as a basic optimization (i.e. under -O/-O1 and higher). Commented Oct 23, 2011 at 18:03
  • When you say "heavily used", be sure you know if you mean a) used in many places in the code, versus b) used enough times per second to account for >10% of the time. The latter argues for inlining, while the former doesn't. Commented Oct 23, 2011 at 18:06

5 Answers 5

5

Yes you probably should. The good use case for the inline keyword in c++ is: small functions, heavily used.

See also http://msdn.microsoft.com/en-us/library/1w2887zk%28v=vs.80%29.aspx

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

Comments

5

Remember that using inline is never a guarantee, it just gives a hint to the compiler. I doubt inlining will actually increase executable size a lot, the function is very small in itself.
Calling the function is almost the same size as the function itself.

Comments

3

Apply inline to all functions that you define in your header on namespace scope to avoid breaking the One Definition Rule. This, by the way, is completely unrelated to inlining, despite the keyword name. (Or put them inside an anonymous namespace.)

inline also gives a hint to the compiler to inline calls to said function, but as the comments have pointed out the compiler is quite capable of figuring that out by itself so the keyword isn’t really needed for that.

Comments

2

The compiler is perfectly capable of making a decision as to whether to inline a function or not depending on the chosen optimization profile.

You should inline a function if the compiler doesn't, and profiling with a realistic data set shows you're spending a significant amount of time in the function, the algorithm using said function is an efficient one, and if inlining it shows a speed improvement in a benchmark with said data set.

Comments

1

If in doubt, compile it with and without inline and compare execution speed and size. The compiler usually offers a switch for profiling as mentionend above to see what the function call costs, measured in time,

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.