Are there any performance impacts (positive or negative) when binding functions (using Boost Bind) ?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Maybe, may not be. It depends.
The result of
std::bind
(or alsoboost::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 ofstd::function
(orboost::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. Bothbind
andfunction
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 usingbind
(for example when binding member function pointers to instances and arguments). Usebind
freely, but conversion tofunction
only if you truly need to manage a heterogeneous collection of callable entities.Here are two typical examples:
Bad; avoid this:
Better; prefer this:
boost::bind
andstd::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 tostd::bind
.You can think about it similarly to creating a tuple of all the arguments, e.g.
should be about equivalent in performance to:
If you don't want the arguments to be copied, use the
ref
utility to pass them in areference_wrapper
which is a very lightweight type that stores a pointer to the object:When calling the bound function, each of the bound arguments will be passed to the bound function, as lvalues (i.e. no perfect forwarding):
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:or passing them by reference:
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.