问题
Are there any performance impacts (positive or negative) when binding functions (using Boost Bind) ?
回答1:
Maybe, may not be. It depends.
The result of std::bind
(or also boost::bind
) is a so-called "bind expression", which has an unknowable type determined by the implementation. This type is a Callable, and it is convertible to an instance of std::function
(or boost::function
).
Internally, function
(may) use type erasure to handle various complex, stateful "callable objects". This entails a dynamic allocation and a virtual dispatch in some (though not necessarily all) cases. Both bind
and function
are stateful, since they store the bound arguments.
The upshot is that you should avoid converting a bind expression to a function
object if possible. The bind expression itself may be cheaper, and you should not be afraid of using bind
(for example when binding member function pointers to instances and arguments). Use bind
freely, but conversion to function
only if you truly need to manage a heterogeneous collection of callable entities.
Here are two typical examples:
Bad; avoid this:
std::function<int(bool, char)> f = std::bind(&Foo::bar, x, 12);
void do_something(std::function<int()> func, int & acc)
{
acc += func();
}
Better; prefer this:
auto f = std::bind(&Foo::bar, x, 12); // unknowable type, but perfectly fine
template <typename F>
void do_something(F && func, int & acc) // can deduce unknowable types
{
acc += func();
}
回答2:
boost::bind
and std::bind
will copy their arguments so that the returned object contains a copy of each argument, including the function object. If those arguments are expensive to copy then it will be expensive to pass them to std::bind
.
You can think about it similarly to creating a tuple of all the arguments, e.g.
auto b = std::bind(func, arg1, arg2, arg3);
should be about equivalent in performance to:
auto b = std::make_tuple(func, arg1, arg2, arg3);
If you don't want the arguments to be copied, use the ref
utility to pass them in a reference_wrapper
which is a very lightweight type that stores a pointer to the object:
auto b = std::bind(func, std::ref(arg1), arg2, arg3);
When calling the bound function, each of the bound arguments will be passed to the bound function, as lvalues (i.e. no perfect forwarding):
b(); // equiv to std::get<0>(b)(std::get<1>(b), std::get<2>(b), std::get<3>(b))
If the function takes its arguments by value, then the bound arguments will be copied into the function arguments. That could be expensive, but it's exactly the same whether you call the function directly or call it inside the result of std::bind
... it's a property of the function being called, not the bind expression.
So the only overhead of using boost::bind
is in the initial copying of the bound arguments, which you can control either by moving the arguments in to avoid copying:
auto b = std::bind(func, std::move(arg1), arg2, arg3);
or passing them by reference:
auto b = std::bind(func, std::ref(arg1), arg2, arg3);
The above discussion ignores the features of bind
such as placeholders and calling nested bind expressions, but those do not affect performance and everything above still applies.
来源:https://stackoverflow.com/questions/12446596/c-boost-bind-performance