2

Possible Duplicate:
When should 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?

Community
  • 1
  • 1

6 Answers6

3

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:

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
2

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
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I mean when should I consider to use it –  Jul 21 '11 at 08:03
  • 2
    You don't - let the compiler do it. The compiler will happily ignore every inline keyword you write and do its own thing if it thinks it's for the better. – R4D4 Jul 21 '11 at 08:15
  • @Als As far as I know, you need to leave the function code in the header file to allow the compiler to inline it (it can't inline a function defined elsewhere). Is that still true? – quant_dev Jul 21 '11 at 08:17
  • @quant_dev: With member functions, you can keep the definition out of the class/struct declaration(using scope resolution) but yes the function definition should be in the header file. – Alok Save Jul 21 '11 at 08:32
  • 2
    @quant - Newer compilers, like gcc and MSVC, can inline functions across compilation units, and do that whether you mark the functions inline or not. The most important use of `inline` is to **allow** you to put functions in headers. – Bo Persson Jul 21 '11 at 08:37
  • Way too many people go overboard with the modern compilers are magical and do amazing things suggestion. For one, you have to at least put the function in the header so the compiler can inline between translation units – ericcurtin Mar 25 '21 at 10:16
2

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.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
2

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

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.

Sriram
  • 10,298
  • 21
  • 83
  • 136
1

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.

Offirmo
  • 18,962
  • 12
  • 76
  • 97