문제

I've witnessed posts in SO mentioning inlining has made their software run significantly faster [example].
It made me wonder, when should I make a function inlined for performance?

Also, would the same guidelines for C++ inlining apply?

Note:
I'm not asking when (obviously when needed), or why, I'm asking that if I happen to reach a bottleneck, when would inlining could be helpful?

도움이 되었습니까?

해결책

In my experience, the inline keyword is mainly useful in two situations:

  • It lets you write generic numeric code which would not be possible without inline. So in this case, you need inlining regardless of the performance concerns.

  • I think that generic comparison (see also this SO question) is the main use case where inline significantly improves performance (because the inlined code can be specialized for the current type rather than using generic comparison).

In all other cases, adding inline might make code a little bit faster, but probably won't have significant effect. So I would only use it when you are actually optimizing some bottleneck.

다른 팁

if I happen to reach a bottleneck, when would inlining could be helpful?

This is the method I use to look for speedups.

If I see that the program counter is in the process of entering or leaving a method X or several methods similar to X on a significant fraction of samples, then it could be worthwhile to inline them.

This will only happen on very short methods that don't perform further calls within them.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top