Possible Duplicate:
When 开发者_如何学Goshould I write the keyword 'inline' for a function/method?
I have a question about inline functions in c++. I know inline functions are used to replace each apperance with the inline function body. But I do not know when to use it. It is said that inline functions can improve performance, but when should I consider using an inline function?
Inline functions are best for small stubs of code that are performance-critical and reused everywhere, but mundane to write.
However, you could also use them to split up your code, so rather than having a 1000-line long function, you may end up splitting this up into a couple of 100-line long methods. You could inline these so it would have the same effect as a 1000-line long method.
Modern compilers will automatically inline some of your smaller methods, however you can always manually inline ones that are bigger.
There are many resources about this, for example:
- http://msdn.microsoft.com/en-us/library/1w2887zk(v=vs.80).aspx
- http://www.cplusplus.com/forum/articles/20600/
- http://www.glenmccl.com/bett_007.htm
- http://www.exforsys.com/tutorials/c-plus-plus/inline-functions.html
Usually modern compilers are intelligent enough to inline certain small functions to a limit (in increment of space). You can provide the hint. Of course, you'll inline
small, repetitive functions that save the overhead of the call.
Note also, that sometimes, even when you provide the inline
keyword, the compiler can decide not to inline that function, so its use (for optimization purposes, at least) is somewhat informative.
Most of the modern compilers are smart enough to apply optimization of inlining functions, Probably best to let the compiler decide it.
inline is just a suggestion and the compiler is free to reject it or apply it.
You might consider making a function inline if:
- A function is small
- You can use inline function instead of a preprocssor directive
#define
The purpose of the inline
keyword is to allow you to define the same function in multiple compilation units (usually by defining it in a header file included from multiple source files). On some compilers, this is essential if you want the compiler to be able to consider it for inlining in more than one compilation unit.
So, if you define a function in a header file, then you should always declare it inline
, otherwise you'll get link errors if the file is included from more than one source file. (If it's a class member function, you could define it inside the class instead; that implicitly declares it inline
). If you define it in a source file, then you can declare it inline
if you think it's a good candidate for inlining (e.g. if it's small, or only called from one place) if you like.
However, compilers generally think they have a better idea which functions than you do; some may take an inline
declaration as a hint, and others may completely ignore it. Some may provide non-standard extensions to force functions to be (or not to be) inlined; only use those if you're sure (by measurement) that it will give an improvement over the compiler's decision, as unwise inlining can hurt performance.
You should use an inline function when you think that repeated calls to the function would take more time than simply placing the body of the function within the main code (in other words, when the body of the function is small and the function gets called repeatedly). However, compilers generally optimize code and may ignore functions that have been defined as inline. There are many other threads on SO that deal with this in greater detail.
Everything said before looks correct, I just want to warn you about a common mistake since you speak about performance.
Some programmers incorrectly think that "inline" = faster because the overhead of the function call is saved. Yes, the overhead is saved, but if your inline function compile into a bigger code than a function call (which happen very quickly), the whole code will get bigger. It then increase the probability that your code will not fit in the processor cache. And cache is everything today... So your "inlined" code will actually run more slowly...
I would say the use of "inline" is only OK for trivial getter/setter functions written directly in the .h.
For everything else, I would advise not to use "inline" and let the compiler decide itself.
And a general advice : apart from the general conception, you should not think about optimisation until everything run and you can make measurements of which operations takes the process time. It usually is less than 20% of the code, so you don't waste your time blindly optimizing everything else. And with measurements, you can immediately see if an optimisation (for example adding some inline here and there) actually works or not.
精彩评论