qepwqnp 发表于 2013-2-5 08:58:22

boost::shared_ptr的性能

原文出版:http://www.darkbull.net/
在最近的项目中,使用了大量的boost::shared_ptr。在游戏服务器中,有时候同一数据块要发送给不同的连接(例如A玩家的一个行走消息包要发给所有附近的玩家),如果为每个连接都拷贝一份数据,显的代价比较大且难以维护,想想发送完数据之后还要释放这些数据块,极易出错,而且性能也不佳。使用智能指针,只需要一份数据块的拷贝,并且不需要去管理内存的释放。但这几天,我又在想,boost::shared_ptr号称是线程安全的,那么在低层维护对象的引用计数时,必定有加琐/解琐。如果频繁的对智能指针进行复制,性能开销也不能忽略。我现在对boost的线程库使用还不是很熟练,网上查了一下,shared_ptr好像是通过Boost.Thread的mutex对象来保护引用计数,如果真是这样,那么我推断:在windows上使用shared_ptr,性能肯定没有直接使用windows api来进行同步的智能指针性能高。基于这样的猜测,我自己写了一些简单的智能指针,通过windows api Interlocked系列函数来维护智能指针引用计数。大概代码如下:
#ifndef AUTOPTR_H_INCLUDED#define AUTOPTR_H_INCLUDED#include <windows.h>template<typename T>class AutoPtr{public:    AutoPtr(void)      : _ptr(0), _refCount(0)    { }    explicit AutoPtr(T *ptr)      : _ptr(ptr)    {      _refCount = new long(1);    }    AutoPtr(const AutoPtr &rhs)      : _ptr(rhs._ptr), _refCount(rhs._refCount)    {      if (_ptr && _refCount)            ::InterlockedIncrement(_refCount);// 引用计数加1    }    ~AutoPtr(void)    {      if (!_ptr)            return;      long refCount = ::InterlockedExchangeAdd(_refCount, -1);      if (refCount == 1)      {            delete _refCount;            delete _ptr;      }    }    T &operator*(void)    {      return *_ptr;    }    const T&operator*(void) const    {      return *_ptr;    }    T *operator->(void)    {      return _ptr;    }    const T*operator->(void) const    {      return _ptr;    }    const AutoPtr &operator=(const AutoPtr &rhs);//    T *get(void)//    {//      return _ptr;//    }////    const T *get(void) const//    {//      return _ptr;//    }////    T *release(void)//    {////    }////    void reset(T *ptr)//    {////    }private:    T *_ptr;    long *_refCount;};template<typename T>const AutoPtr<T> &AutoPtr<T>::operator=(const AutoPtr &rhs){    if (_ptr)    {      long refCount = ::InterlockedExchangeAdd(_refCount, -1);      if (refCount == 1)      {            delete _refCount;            delete _ptr;      }    }    _ptr = rhs._ptr;    _refCount = rhs._refCount;    if (_refCount)      ::InterlockedIncrement(_refCount);    return *this;}#endif // AUTOPTR_H_INCLUDED 并写了一个简单的测试程序,用来对比自己写的AutoPtr,boost::shared_ptr,std::tr1::shared_ptr。测试代码如下:
int main(int argc, char *argv[]){AutoPtr<int> intPtr1(new int(0));    time_t beg1 = clock();    for (int i = 0; i < 10000000; ++i)    {      AutoPtr<int> tmp = intPtr1;      ++(*tmp);    }    time_t end1 = clock();std::cout << "AutoPtr cost: " << end1 - beg1 << ", Result: " << *intPtr1 << std::endl;boost::shared_ptr<int> intPtr2(new int(0));time_t beg2 = clock();for (int i = 0; i < 10000000; ++i){ boost::shared_ptr<int> tmp = intPtr2; ++(*tmp);}time_t end2 = clock();std::cout << "boost::shared_ptr cost:" << end2 - beg2 << ", Result: " << *intPtr2 << std::endl;std::tr1::shared_ptr<int> intPtr3(new int(0));time_t beg3 = clock();for (int i = 0; i < 10000000; ++i){std::tr1::shared_ptr<int> tmp = intPtr3; ++(*tmp);}time_t end3 = clock();std::cout << "std::tr1::shared_ptr cost:" << end3 - beg3 << ", Result: " << *intPtr3 << std::endl;return 0;} 结果出乎我的意料:
在VC9下的结果为:
http://yellow-bull.appspot.com/File/1281888203
在gcc4.4.2下的结果:
http://yellow-bull.appspot.com/File/1281888255
结果表明,boost:;shared_ptr的性能并没有我想像是的那么差,在vc9下,三种智能指针的性能在同一水平上;而在gcc4.4.2下,boost::shared_ptr与AutoPtr性能相近,但与tr1版本的shared_ptr相差一个数量级。
页: [1]
查看完整版本: boost::shared_ptr的性能