Are there any performance impacts (positive or negative) when binding functions (using Boost Bind) ?
Are there any performance impacts (positive or negative) when binding functions (using Boost Bind)
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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. Bothbindandfunctionare stateful, since they store the bound arguments.The upshot is that you should avoid converting a bind expression to a
functionobject 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). Usebindfreely, but conversion tofunctiononly if you truly need to manage a heterogeneous collection of callable entities.Here are two typical examples:
Bad; avoid this:
Better; prefer this: