• 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

  • 相关阅读:
    01 《i》控制字体大小 v-for循环绑定类名 v-bind 结合三目运算 动态添加类
    右侧是长方形和半圆结合 光标放上去在规定时间内完成动画
    04-align-content 它对于当单行是没有效果的
    03-flex-wrap是否换行
    02-align-items的用法
    01--顶部的通告特效---仅显示一条一条滚动
    洛谷P2392 kkksc03考前临时抱佛脚(01背包/搜索)
    蓝桥杯 9大臣的旅费(树的直径)
    蓝桥杯 8买不到的数目(数论/线性DP)
    蓝桥杯 7连号区间数(暴力or并查集(?)
  • 原文地址:https://www.cnblogs.com/len3d/p/993698.html
Copyright © 2020-2023  润新知