是否提高::绑定事业开销? [英] Does boost::bind cause overhead?
问题描述
我目前工作的网络软件。它有一个主类,服务器
这显然重新presents的服务器实例。
I am currently working on network software. It has one main class, server
which obviously represents a server instance.
A 服务器
实例可以发送请求,并通知用户用回调的响应。
A server
instance can send requests and the user is notified of the response by a callback.
在code是这样的:
class server
{
public:
typedef boost::function<void (int duration)> callback_func;
void send_request(endpoint& ep, callback_func cb);
};
现在让我们说,作为一个用户我想回调了解调用它的情况下,我可以做以下的事情:
Now let's say that, as a user I want to the callback to know about the instance that called it, I can do the following thing:
void mycallback(const server& sv, int duration) { ... }
server sv;
sv.send_request("localhost", boost::bind(&mycallback, boost::ref(sv), _1));
但我不知道:有没有什么开销,这样做?在调用 myCallBack函数
会有什么比使用较慢的常规打电话?
But I wonder: is there any overhead doing so ? Will the calls to mycallback
be any slower than using a "regular" call ?
感谢您。
的脚踏注:当然,我可以改变我的的typedef
为:的typedef的boost ::功能&LT;无效(const的服务器和放大器; SV, INT持续时间)&GT; callback_func;
,如果的boost ::绑定
造成任何显著的开销,这可能是什么,我会做到底。我只是想知道是什么费用意味着使用的boost ::绑定
的的
Foot note: I could of course change my typedef
to: typedef boost::function<void (const server& sv, int duration)> callback_func;
and if boost::bind
cause any significant overhead, that's probably what I will do in the end. I would just like to know what costs implies the use of boost::bind
.
推荐答案
当然,这会导致开销。它所做的是,它会创建一个存储绑定参数并有一个运营商算符()
你用剩下的参数来调用。现在,这是显著?我不知道,因为这只是一个字。让1000万个请求,并测量它。你是唯一一个谁可以告诉我们,如果这方面的开销是给你显著与否。
Of course it causes overhead. What it does is that it creates a functor that stores the bound parameters and has an operator()
that you call with the remaining arguments. Now, is this significant? I don't know, because that is just a word. Make 10 million requests and measure it. You are the only one who can tell if this overhead is significant to you or not.
另外,我面临着类似的问题。由于我没有真正需要的代表,我可以摆脱函数指针。但我发现一个有趣的基准也提到了一些替代实现,所有的人都具有更好的性能比的boost ::功能
。这是以便携性的成本,在执行某些情况下丑陋的,非标准的黑客(但换来一个非常不错的表现)。
Also, I faced a similar problem. Since I didn't really need delegates, I could get away with function pointers. But I found an interesting benchmark which also mentions some alternative implementation, all of them with better performance than boost::function
. This comes at the cost of portability and in some cases ugly, non-standard hacks in the implementation (but a really good performance in return).
这篇关于是否提高::绑定事业开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!