a simple test of MemMan

内存管理测试
本文介绍了一个简单的内存管理测试程序,该程序使用自定义内存管理器来分配和访问大量页面对象。测试涉及随机分配不同大小的页面,并多次访问这些页面,评估内存管理效率。

 

#include  " stdafx.h "
#include 
" memman3_def.h "

using   namespace  std;

class  Page  {    //    16
public:
    
int        author;
    
int        date;
    
int        title;
    
int        text;
}
;

class  Wiki :  public  mem::resource  {
public:
    Wiki() : mem::resource( 
2 )
    
{
    }


    
bool stream_in()
    
{
        
if!pages )
        
{
            allocate( num );
            
return true;
        }

        
return false;
    }


    mem::size_type stream_size()
    
{
        
if( pages )
            
return sizeof(Page) * num;
        
else
            
return 0;
    }


    
void *stream_out()
    
{
        
void    *ptr = (void *) pages;
        pages 
= NULL;
        
return ptr;
    }


    
void allocate( int size )
    
{
        num 
= size;
        pages 
= (Page *) alloc( sizeof(Page) * num );
    }


    
void access_resource()
    
{
        stream_begin();

        
/**//////// DO YOUR ACCESS HERE! ///////

        
////////////////////////////////////


        stream_end();
    }


    
void deallocate()
    
{
        dealloc( pages, 
sizeof(Page) * num );
    }


public:
    Page    
*pages;
    
int        num;
}
;

const   int  access_size  =   100000000 ;
const   int  test_size  =   100000 ;    
const   int  alloc_size  =   1000 ;
const   int  heap_size  =   500000000 ;

int         randnum(  int  size )
{
    
return max( int(((float)rand() / (float)RAND_MAX) * (float)size), 1 );
}


int  _tmain( int  argc, _TCHAR *  argv[])
{
    Wiki    
*wiki;

    memman.begin( heap_size );

    wiki 
= new (memman.alloc( sizeof(Wiki)*test_size )) Wiki [test_size];

    
for(int i=0;i<test_size;++i)
        wiki[i].allocate( randnum( alloc_size ) );

    
forint i = 0; i < access_size; ++i )
    
{
        
int index = randnum( test_size );

        
if( index >= 0 && index < test_size )
            wiki[ index ].access_resource();
    }


    memman.print();

    
for(int i=test_size-1;i>=0;--i)
        wiki[i].deallocate();

    memman.dealloc( wiki, 
sizeof(Wiki)*test_size );

    memman.end();

    
int    a;
    cin 
>> a;

    
return 0;
}

heap allocated size: 476.837 MB
page size: 524288 Byte
cache allocated size: 880.509 MB
number of cache lookups: 100000000
cache hit rate: 99.9854 %
run time: 1 minutes 27 seconds

posted on 2007-12-13 14:35 Len3d 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/len3d/archive/2007/12/13/993698.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值