(搬运工)在std::vector下,不同内存分配方式效率简单比较

#include  " stdafx.h "
#include 
< iostream >
#include 
< vector >
#include 
< time.h >
#include 
< boost / pool / pool_alloc.hpp >
template
< typename T >
class  alloc 
{
public :
    typedef std::size_t     size_type;
    typedef std::ptrdiff_t  difference_type;
    typedef T
*     pointer;
    typedef 
const  T *  const_pointer;
    typedef T
&     reference;
    typedef 
const  T &  const_reference;
    typedef T     value_type;
public :
         alloc(){}
     template
< typename T >
     alloc(
const  alloc < T >&  ){
     }
     alloc
&   operator = ( const  alloc &  rhs){
         
return   * this ;
     }
     
static  pointer allocate(size_type n)
     {
         
return  (pointer)malloc(n * sizeof (T));
     }
    
static   void  deallocate(pointer ptr, size_type n)
    {
        free((pointer)ptr);
    }
    size_type max_size() 
const   throw () 
    { 
        
return  size_t( - 1 /   sizeof (T);
    }
    template 
< typename U >
    
struct  rebind
    { 
        typedef alloc
< U >  other;
    };
    
};
int  _tmain( int  argc, _TCHAR *  argv[])
{      
    
    
int  start4  =  GetTickCount();
    std::vector
< int , alloc < int >>  RR4;
    
for  ( int  i  =   0 ; i  <   10000000 ; i ++ )
    {
        RR4.push_back(i);
        
    }
    
int  time4  =  GetTickCount() -  start4;
    std::cout
<< " alloc:  " << time4 << std::endl;
    
int  start3  =  GetTickCount();
    std::vector
< int , boost::fast_pool_allocator < int >>  RR3;
    
for  ( int  i  =   0 ; i  <   10000000 ; i ++ )
    {
        RR3.push_back(i);
    }
    
int  time3  =  GetTickCount() -  start3;
    std::cout
<< " boost::fast_pool_allocator:  " << time3 << std::endl;
    
int  start1  =  GetTickCount();
    std::vector
< int ,std::allocator < int >>  RR1;
    
for  ( int  i  =   0 ; i  <   10000000 ; i ++ )
    {
        RR1.push_back(i);
    }
    
int  time1  =  GetTickCount() -  start1;
    std::cout
<< " std::allocator:  " << time1 << std::endl;

    
int  start2  =  GetTickCount();
    std::vector
< int , boost::pool_allocator < int >>  RR2;
    
for  ( int  i  =   0 ; i  <   10000000 ; i ++ )
    {
        RR2.push_back(i);
    }
    
int  time2  =  GetTickCount() -  start2;
    std::cout
<< " boost::pool_allocator:  " << time2 << std::endl;
    
return   0 ;
}

debug (VC2008):

[c-sharp] view plain copy print ?
  1. alloc: 11438
  2. boost::fast_pool_allocator: 19437
  3. std::allocator: 11562
  4. boost::pool_allocator: 27703
  5. 请按任意键继续. . .
alloc: 11438 boost::fast_pool_allocator: 19437 std::allocator: 11562 boost::pool_allocator: 27703 请按任意键继续. . .

 

Release(VC2008):

[c-sharp] view plain copy print ?
  1. alloc: 375
  2. boost::fast_pool_allocator: 1032
  3. std::allocator: 343
  4. boost::pool_allocator: 1266
  5. 请按任意键继续. .

你可能感兴趣的:((搬运工)在std::vector下,不同内存分配方式效率简单比较)