I have a little bit problem,and that is when i use inline function? Another one is advantage and disadvantage of inline function.

Anyone help me?

Your Regard
Sohel Rana
Bangladesh

Edited 6 Years Ago by Sohelp: n/a

I believe the idea of inline functions is that it will make small functions run faster. It seems like an "old school" concept - can someone testify that it still makes a significant difference on modern compilers? Maybe provide a short example program and some timings? This seems like a good candidate for something to go in the 'code snippets' or 'tutorials' section of the forum.

Hmm... I don't know... I would use it if the function is very short and straight forward. Not sure what the advantage is...

Generally, an inline function is requested when it's a short function (as in only 4 or 5 lines) and you don't want the additional execution overhead associated with a function call. This makes for quicker execution, but that's offset by a larger program because of all the extra code expansion.

The reason I say requested is that the inline keyword (like the "register" keyword) is simply a suggestion/request to the compiler concerning your intentions. The compiler however, is not required to actually produce the function as an inline function, it is free to ignore it if it so chooses.

The inline function makes sure the variable is stored in a different place in the memory, so that the computer can access it very quickly.
It's mostly used on old computers, that are quite slow, with programs like these:

///....

inline void func () {
for (int n; n <100000; n++)
printf ("\n 'n' = %d",n);

}

However, the compiler nowadays automaticly assigns which functions get to be inline.
So there's no need to type it.

>>The inline function makes sure the variable is stored in a different place in the memory, so that the computer can access it very quickly.
Incorrect. Not only is it not a variable, it's a function. It does not change it's storage location, that is still a function call with the associated overhead.

When the compiler chooses to follow the inline directive the code produced works more like a function-like macro. The contents of the inlined function are expanded directly in the compiled code so that the flow of processing doesn't have to bounce to a different location in memory then back to the original location. Not having to perform those jumps saves processing cycles. That's the whole point if inlining. The problem is, this is accomplished at the expense of program size. A "successful" inline can raise the memory requirements of the program because it makes the executable bigger due to repetitive code.

Edited 6 Years Ago by Fbody: n/a

There are no simple answers. http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.3

In my experience, a good function to inline is one
i. which has a stable (unlikely to change) implementation.
ii. which is frequently called (the program spends a signifanct portion of its time in calling and executing the function).
iii. which has a code size comparable to the code size required to set up the activation record and call the function.

The compiler or the linker is in no position to make a judgement about i.

Unless an implementation has a good profile-guided optimizer, and the developer profiles the unoptimized program in a production environment, and makes the profiling information available to such a compiler, it is in no position to make a judgment about ii.

In reality, determining correctly if it is beneficial to inline a function requires knowledge that the compiler or linker does not have at build-time. A function that is worthwhile to inline when running on a processor with say a 4 MB processor cache and 4 GB memory (with no other serous load other than the program in question), may be harmful to inline if
i. You take it to a machine with a 2 MB processor cache and 2 GB of memory.
ii. The same 4 MB cache / 4 GB memory machine has several other programs also running simultaneously, and making demands on memory.


In general, the rules I follow are:
i. Do not inline by default. Consider inlining only if performance requirements mandate it.
ii. For order of magnitude improvements in performance, look at the data structures and algorithms first.
iii. Profile the code in a real-life environment to identify candidate functions for inlining.
iv. Choose the candidate set based on issues mentioned earlier.
v. Inline incrementally and profile the code at each stage.
vi. Disallow 'automatic inlining' by the compiler or linker when I can't afford to have them making ill-informed decisions about what are the functions that are good for inlining.

For me, inline function is used in the situation where it will only be called from one place. The main purpose for me to implement it as an inline function is solely readability and design. That's when my function is getting too long and there are too many nested conditional checking. Frankly speaking, I do not see any performance difference. Maybe someone can prove it.

Thank you all of my Friend
Thank you very much.

Your Regard
Sohel Rana
Bangladesh

This article has been dead for over six months. Start a new discussion instead.