tcmalloc性能测试对比

tcmalloc性能测试对比
使用tcmalloc测试多线程分配对象
#include  < boost / thread.hpp >
#include 
< boost / bind.hpp >
#include 
< boost / shared_ptr.hpp >
#include 
< boost / progress.hpp >
#include 
< vector >


template 
< int  N >
class  TestObjects
{
public:
    

private:

   
enum { size = N*1024};

   
char m_content[size];

}
;

template
< int  N >
void  mallocobjectfunc()
{
    typedef TestObjects
<N>  TObject;
    
int n = 0;
    
for(; n < 100 ; n++)
    
{
        
{
            std::vector
< boost::shared_ptr<TObject > >  m_vobjc;
            
            m_vobjc.reserve(
10000);
            
            
int i = 0;
            
for(; i < 10000 ; i++)
            
{
                boost::shared_ptr
<TObject> p(new TObject);
                m_vobjc.push_back(p);
            }
    
        
        }

    }

}



int  main()
{
    
//单线程测试
    
//测试小于32K 小对象
    {
        boost::progress_timer pt;
        mallocobjectfunc
<14>();
    }

    
    
//测试大于32K 小对象
    {
        boost::progress_timer pt;
        mallocobjectfunc
<40>();
    }

    
    

    
//多线程
    
//测试小于32K 小对象
    {
        boost::progress_timer pt;
    
        boost::thread t1(boost::bind(
&mallocobjectfunc<12>));
        boost::thread t2(boost::bind(
&mallocobjectfunc<12>));
        boost::thread t3(boost::bind(
&mallocobjectfunc<12>));
        boost::thread t4(boost::bind(
&mallocobjectfunc<12>));
        boost::thread t5(boost::bind(
&mallocobjectfunc<12>));
        
        
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        t5.join();
    }

    
    
        
//测试大于32K 小对象
    {
        boost::progress_timer pt;
    
        boost::thread t1(boost::bind(
&mallocobjectfunc<35>));
        boost::thread t2(boost::bind(
&mallocobjectfunc<35>));
        boost::thread t3(boost::bind(
&mallocobjectfunc<35>));
        boost::thread t4(boost::bind(
&mallocobjectfunc<35>));
        boost::thread t5(boost::bind(
&mallocobjectfunc<35>));
        
        
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        t5.join();
    }

    
    
return 0;

}













测试结果:

                        glibc           tcmalloc

单线程小内存       2.46 s         0.96 s

单线程大内存       2.59 s         1.11 s

多线程小内存      104.96 s     7.77 s

多线程大内存      143.13 s     18.74 s



可见在多线程小内存申请,tcmalloc效率比glibc效率提高了13倍。


你可能感兴趣的:(tcmalloc性能测试对比)