When to make a function inline?

Started by
3 comments, last by brewknowc 21 years, 10 months ago
Hi, I read in a few places that making code inline can speed up the game because it no longer needs the function call. So I have 2 questions: 1) Why not use this for everything? (kinda stupid, but just wondering) 2) When SHOULD you use it? Thanks - Free Your Mind -
- Free Your Mind -
Advertisement
only use inline when the function is very short.

eg:

inline int Ship::GetX()
{
return m_x;
}
quote:Original post by brewknowc
1) Why not use this for everything? (kinda stupid, but just wondering)

Because it doesn''t always result in a performance improvement, and would actually result in bloat.
quote:
2) When SHOULD you use it?

Never. Your compiler can make the decision for itself, and quite possibly does a reasonable job of it.
quote:Original post by SabreMan
Because it doesn''t always result in a performance improvement, and would actually result in bloat.

Plus it can result in incorrect code (from your standpoint) if the compiler decide not to inline it and it contains a static variable.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ok my answer will be copy past !


Rule of Thumb:
If your function is a small function one or too statements
it is a candidate for inline. when in doubt, leave it out.

1º NOTE: Inline is just a hint to your compiler that you would like the function to be inlined. The compiler is free to ignore the hint and make a real function call

2º NOTE:
Note that inline functions can bring a heavy cost if a function is called 50 times then the inline code is copy is copyed 50 times to the calling funtion(s). The tiny improvement in speed you might achieve is more than swamped by the increase in the executable program.

this was all copy paste

This topic is closed to new replies.

Advertisement