c + +提升业绩绑定 [英] C ++ Boost Bind Performance
问题描述
(使用Boost绑定)绑定功能时是否有任何性能的影响(正面或负面的)?
Are there any performance impacts (positive or negative) when binding functions (using Boost Bind) ?
推荐答案
也许,也许不是。这要看情况。
Maybe, may not be. It depends.
的std ::绑定
的结果(或者也的boost ::绑定
)是所谓的结合前pression,里面有一个未与害羞;知道&害羞;由实现确定能够类型。该类型是的可赎回的,它是的转换的一个在与害羞; 的std ::功能
的立场(或的boost ::功能
)。
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
).
在内部,函数
(5月)使用类型擦除处理各种复杂,有状态的呼叫&害羞,能够对象。这需要动态分配,并在某些虚拟讯(虽然不是neces&害羞;纱丽和害羞; LY全部)的情况。无论绑定
和函数
是有状态的,因为它们存储绑定参数。
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.
其结果是,你应该避免绑定前pression转换为函数
对象,如果可能的话。绑定前pression本身可能会更便宜,你不应该害怕使用绑定
(例如,当绑定与害羞; ING成员函数指针的实例和参数)。使用绑定
自由,但转化为函数
只有当你真正需要管理的可调用实体的异类集合。
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:
坏;避免这种情况:
std::function<int(bool, char)> f = std::bind(&Foo::bar, x, 12);
void do_something(std::function<int()> func, int & acc)
{
acc += func();
}
更好; preFER这样的:
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();
}
这篇关于c + +提升业绩绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!