inline functions

A

asdf

When and where should I use inline functions?

I know any member functions inside classes are automatically inline
function, how about the functions outside the classes?

The inline functions is said to avoid the overhead of using the
ordinary functions. So, do I need to use inline functions as more as
possible?
 
A

amparikh

asdf said:
When and where should I use inline functions?

Whenever it is possible, you might use inline functions. Having said
that you have ato remember a few things.

1>Making a function inline doesnt always guarentee you a better
performance. for e.g when there is page thrashing. Read the FAQ at
www.parashift as it gives some expalaination.
of course, you can find lots of article if you do a search.

2>The compiler might choose not to inline it even if you define the
function to be inline.
Certain compilers give you an option to force inline which leaves the
discretion to the programmer. For example Microsoft compilers have the
__forceinline directive.
I know any member functions inside classes are automatically inline
function, how about the functions outside the classes?

If you declare functions defined outside the class as inline, they
"could" be inlined.
The inline functions is said to avoid the overhead of using the
ordinary functions. So, do I need to use inline functions as more as
possible?

Yes and No. Go back to the first 2 points I made above and read the FAQ.
 
A

asdf

thanks for your help.

Whenever it is possible, you might use inline functions. Having said
that you have ato remember a few things.

1>Making a function inline doesnt always guarentee you a better
performance. for e.g when there is page thrashing. Read the FAQ at
www.parashift as it gives some expalaination.
of course, you can find lots of article if you do a search.

2>The compiler might choose not to inline it even if you define the
function to be inline.
Certain compilers give you an option to force inline which leaves the
discretion to the programmer. For example Microsoft compilers have the
__forceinline directive.


If you declare functions defined outside the class as inline, they
"could" be inlined.


Yes and No. Go back to the first 2 points I made above and read the FAQ.
 
M

Martin Steen

asdf said:
When and where should I use inline functions?

I know any member functions inside classes are automatically inline
function, how about the functions outside the classes?

The inline functions is said to avoid the overhead of using the
ordinary functions. So, do I need to use inline functions as more as
possible?

Inline functions make your code faster but larger.

Faster, because there is no overhead for calling the function.
Larger, because everywhere you call the function the whole function-code
is included.

It's your own decision where to use inline. I only use it on small
functions or operators - and as a replacement for C-style macros.

-Martin
 
T

Torsten Mueller

asdf said:

Beginners learn very often something about inline functions and then
they are sure inline functions are the key to success. All my students
did indeed know this and used "inline" a lot.

Indeed inline functions make a program faster. But on a modern GHz CPU
in event driven GUI applications on multitasking operating systems you
will hardly see a real difference. In most cases your application has
a lot of much more efficient ways to become faster, especially the
improvement of your design and the use of better algorithms. I guess
using "inline" normally leads to less than a promille more speed
(except very special and seldom cases).

Oftenly the compiler even doesn't generate the your inline function as
inline code because it is virtual or too large or it contains a loop
or something. Other compilers generate inline code even if you don't
qualify your function with "inline". I find the generation of inline
code is indeed a compiler optimization. And the compiler itself should
be responsible for that.

The keyword "register" is another relict from former times of early
compilers without good optimizations. Just don't use it anymore except
you really know that this works in a very special case on a specific
processor.

T.M.
 
M

Martin Steen

Torsten said:
Beginners learn very often something about inline functions and then
they are sure inline functions are the key to success.
LOL

Indeed inline functions make a program faster. But on a modern GHz CPU
in event driven GUI applications on multitasking operating systems you
will hardly see a real difference.

That depends on your application. I sometimes write file-converters that
work on files that are more that 100MB large. And you can believe me
that there is a difference if I use inline functions or not.
But I use them only for small functions and operators.

In most cases your application has
a lot of much more efficient ways to become faster, especially the
improvement of your design and the use of better algorithms. I guess
using "inline" normally leads to less than a promille more speed
(except very special and seldom cases).

For me, these "special cases" are my daily work ;)
The keyword "register" is another relict from former times of early
compilers without good optimizations.

That's true. I suppose most modern compilers ignore the register keyword
at all.

Best regards, Martin
 
G

Greg Comeau

When and where should I use inline functions?

I know any member functions inside classes are automatically inline
function, how about the functions outside the classes?

The inline functions is said to avoid the overhead of using the
ordinary functions. So, do I need to use inline functions as more as
possible?

I just added this as food for thought:
http://www.comeaucomputing.com/techtalk/#whentoinline

I'll spit shine it over the next few day as I've probably
misstated something and left out a bunch of stuff.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top