inline

D

D. Webb

What do I gain from defining a [small] function as inline?
Thanks
R

In case you haven't found the answer you were looking for, I'll give it
a shot:

Calling a function has a (small) amount of overhead to it: things
(i.e., return address and function arguments) need to be pushed and
popped off of the stack, and jumps need to be made to different points
in the program.

If a function is "sufficiently" small (only a few lines), then the
overhead of actually calling a function may become a significant
portion of the function's execution time.

By inline'ing the function, a copy of the functions body is compiled
in-place wherever it is called in the program, thus eliminating the
actual call during runtime, and *maybe* getting small performance
increase.

As Malcolm said, this is usually one of the optimizations a compiler
will do automatically for you, but hopefully now you have a better
understanding of why it does it.

D. Webb
 
S

swengineer001

What do I gain from defining a [small] function as inline?
Thanks
R

In case you haven't found the answer you were looking for, I'll give it
a shot:

Calling a function has a (small) amount of overhead to it: things
(i.e., return address and function arguments) need to be pushed and
popped off of the stack, and jumps need to be made to different points
in the program.

If a function is "sufficiently" small (only a few lines), then the
overhead of actually calling a function may become a significant
portion of the function's execution time.

By inline'ing the function, a copy of the functions body is compiled
in-place wherever it is called in the program, thus eliminating the
actual call during runtime, and *maybe* getting small performance
increase.

This is not required at all. According to section 6.7.4 of the
standard "Making a function an inline function suggests that calls to
the function be as fast as possible. The extent to which such
suggestions are effective is implementation-defined." In other words
inline may do as you suggest. It may also perform some other form of
optimization to increase the speed of the function. It may only
perform optimizations for some of your inline functions based on some
criteria defined by the implementer. It it may in fact do nothing at
all.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top