n2ize
01-05-2008, 08:48 PM
In C it is common to define small functions as macros...
#define ADD(x,y) (x) + (y)
The advantage being that the macro is expanded in place resulting in less overhead than placing values on the stack and using CALL.
In C++ macros get tricky due to side effects and the fact that macros can't access private class data members. Thus defining ADD as above then calling it on private class members would cause an error.
Fortunately C++ has "inline functions". They are very easy to implement either by including the "inline" keyword OR by including the function definition along with the function declaration inside the class.
Thus
class Foo {
int bar(int a, int b)
{ return a + b; }
};
would automatically be created as an inline function. The advantage is that the function is expanded in place resulting in less overhead than a standard function call. The downside is that the header file containing the definition needs to be included in every file that uses the function.
The advantage of eliminating function call overhead is not unlimited optimization. Since the inline funtion is expanded in place everywhere the function is used inlining large functions that are called frequently can start to result in bloated code which ultimately diminishes the effect of any optimizations obtained via inlining.
However, one thing that is not made very clear (at least in most C++ texts) is how large is large ? At what point is it best to refrain from inlining functions and instead make standard function callsvia external function definition ?
Wonder if any of you C++ coders out there have done any benchmark tests on code optimized with inline functions versus non-inline function calls ? If so what have you discovered. At what point (how big a function) is it no longer warranted to use inline functions in a C++ program ?
#define ADD(x,y) (x) + (y)
The advantage being that the macro is expanded in place resulting in less overhead than placing values on the stack and using CALL.
In C++ macros get tricky due to side effects and the fact that macros can't access private class data members. Thus defining ADD as above then calling it on private class members would cause an error.
Fortunately C++ has "inline functions". They are very easy to implement either by including the "inline" keyword OR by including the function definition along with the function declaration inside the class.
Thus
class Foo {
int bar(int a, int b)
{ return a + b; }
};
would automatically be created as an inline function. The advantage is that the function is expanded in place resulting in less overhead than a standard function call. The downside is that the header file containing the definition needs to be included in every file that uses the function.
The advantage of eliminating function call overhead is not unlimited optimization. Since the inline funtion is expanded in place everywhere the function is used inlining large functions that are called frequently can start to result in bloated code which ultimately diminishes the effect of any optimizations obtained via inlining.
However, one thing that is not made very clear (at least in most C++ texts) is how large is large ? At what point is it best to refrain from inlining functions and instead make standard function callsvia external function definition ?
Wonder if any of you C++ coders out there have done any benchmark tests on code optimized with inline functions versus non-inline function calls ? If so what have you discovered. At what point (how big a function) is it no longer warranted to use inline functions in a C++ program ?