boost::bind时候注意性能问题

原文地址:http://hi.baidu.com/hpagent/blog/item/471b7d92dc8ac39ca977a4dc.html

测试一下,看看例子

#include <iostream>
#include <string>
#include "boost/bind.hpp"
using namespace std;
class tracer {
public:  
 tracer() 
    {    std::cout << "tracer::tracer()\n";  }  
 tracer(const tracer& other)
 {    std::cout << "tracer::tracer(const tracer& other)\n";  }
 tracer& operator=(const tracer& other)
 {    std::cout <<      "tracer& tracer::operator=(const tracer& other)\n";  
 return *this; 
 } 
 ~tracer() 
 {    std::cout << "tracer::~tracer()\n";  }  
 void print(const std::string& s) const
 {    std::cout << s << '\n';  }
};
int main(void)
{
 {

    tracer t;
  // boost::bind(&tracer::print,t,_1)(string("this is a test"));//这样是用的拷贝构造函数,而且发生了很多次,很消耗资源,1
  // boost::bind(&tracer::print,boost::ref(t),_1)(string("this is a second test"));//这里没有发生,全是引用,2

      boost::bind(&tracer::print,&t,_1)(string("this is a second test")); 3
 }
   system("PAUSE");
}

 

首先运行第一个绑定器,结果非常不满意:

db37e328a23caad6033bf617.jpg
这么多拷贝,很是浪费资源,于是乎,我们可以利用后面两个绑定器,来看看结果:

6e48af20042e92bed7cae2ef.jpg
这样看起来是不是很节省资源呢?

最后两条结果是一样的,但是第一个是引用,第二个是利用的指针,不要混淆了!!!!!!!!!!!!!!!!!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值