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?
5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. 6) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.
inline functions might make it slower: Too much inlining might cause code bloat, which might cause “thrashing” on demand-paged virtual-memory systems. In other words, if the executable size is too big, the system might spend most of its time going out to disk to fetch the next chunk of code.
One should use the inline function qualifier only when the function code is small. If the functions are larger you should prefer the normal functions since the saving in memory space is worth the comparatively small sacrifice in execution speed.
Answer. An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.
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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With