In the wikipedia article about function objects it says such objects have performance advantages when used with for_each because the compiler can "inline" them.
I'm a bit foggy on exactly what this means in this context... or any context I'm embarrassed to say. Thanks for any help!
The inline command lets you create a function of any number of variables by giving a string containing the function followed by a series of strings denoting the order of the input variables.
Compiler supportInlining is done automatically by many compilers in many languages, based on judgment of whether inlining is beneficial, while in other cases it can be manually specified via compiler directives, typically using a keyword or compiler directive called inline .
The inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. Using inline functions can make your program faster because they eliminate the overhead associated with function calls.
Compiling with -Otime increases the likelihood that a function is inlined. Large functions are not normally inlined because this can adversely affect code density and performance.
The last parameter of for_each
template is a functor. Functor is something that can be "called" using the ()
operator (possibly with arguments). By defintion, there are two distinctive kinds of functors:
()
operator (so called function objects) are also functors.Now, if you wanted to use an ordinary function as a functor for for_each
, it would look something like the following
inline void do_something(int &i) { /* do something */ }
int main() {
int array[10];
std::for_each(array, array + 10, &do_something);
}
In this case the for_each
template is instantiated with [deduced] arguments <int *, void (*)(int &)>
. Note that the actual functor value in this case is the function pointer &do_something
passed as the function argument. From the point of view of for_each
function this is a run-time value. And since it is a run-time value, the calls to the functor cannot be inlined. (Just like it is in general case impossible to inline any call made through a function pointer).
But if we use a function object instead, the code might look as follows
struct do_something {
void operator()(int &i) { /* do something */ }
};
int main() {
int array[10];
std::for_each(array, array + 10, do_something());
}
In this case the for_each
template is instantiated with [deduced] arguments <int *, do_something>
. The calls to the functor from inside for_each
will be directed to do_something::operator()
. The target for the call is known and fixed at compile-time. Since the target function is known at compile-time, the call can easily be inlined.
In the latter case we, of course, also have a run-time value passed as an argument to for_each
. It is a [possibly "dummy" temporary] instance of do_something
class we create when we call for_each
. But this run-time value has no effect on the target for the call (unless the operator ()
is virtual), so it doesn't affect inlining.
Inline is the process a compiler can replace a call to a function with the contents of the function itself. It requires the compiler to know the contents of the function when it's being compiled.
The compiler often can't do this if a function pointer is passed.
Inlining just means replacing every call to that function with the body of that function directly.
It's an optimization for small functions because it reduces the overhead of jumping to a new function and then returning.
It means that the function's definition (code) may be copied and saving you from a function call (which is considered to be expensive on some systems). Think of macro replacement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With