一个测试ACE各种内存分配策略效率的程序

来源:互联网 发布:网络安全技术自学 编辑:程序博客网 时间:2024/05/17 05:01
在256M内存的redhat9虚拟机上运行申请释放10000000,每次大小为1k,结果如下:
testmalloc loop 10000 use time 32.950000 s
testcached loop 10000 use time 1.730000 s
testmallocmmap loop 10000 use time 5.050000 s
testmallocmmaplite loop 10000 use time 5.030000 s
testmalloclocal loop 10000 use time 0.620000 s
testmalloclocalmutex loop 10000 use time 4.250000 s
testmalloc_brk_mutex loop 10000 use time 0.600000 s
testmallocsharemutex loop 10000 use time 4.360000 s
 
 
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/Malloc_T.h"
#include "ace/Null_Mutex.h"
#include "time.h"
#include "assert.h"
 
clock_t _begin;
clock_t _end;
int _loop;
#define BEGIN(n) /
    _begin = clock(); /
    for (_loop = 0; _loop < n; _loop++) {
 
#define END(s) /
    }          /
    _end = clock(); /
    printf("%s loop %d use time %lf s/n", s, _loop ,  (double)(_end - _begin) / (double)CLOCKS_PER_SEC);
 
const int MAXSIZE = 1024;
 
void *p[1000];
void testmalloc()
{
    int i = 0;
    for (i = 0; i < 1000; i++)
    {
        p[i] = malloc(MAXSIZE);
        assert(p[i] != NULL);
    }
    for (i = 0; i < 1000; i++)
    {
        free(p[i]);
    }
}
 
typedef char BLOCK[MAXSIZE];
typedef ACE_Dynamic_Cached_Allocator<ACE_SYNCH_NULL_MUTEX> Allocator;
 
Allocator cache(1000, MAXSIZE);
void testcached()
{
    int i = 0;
    for (i = 0; i < 1000; i++)
    {
        p[i] = cache.malloc(MAXSIZE);
        assert(p[i] != NULL);
    }
    for (i = 0; i < 1000; i++)
    {
         cache.free(p[i]);
    }
}
 
typedef ACE_Malloc<ACE_MMAP_MEMORY_POOL, ACE_SYNCH_MUTEX> malloc_mmap;
typedef ACE_Malloc<ACE_LOCAL_MEMORY_POOL, ACE_SYNCH_NULL_MUTEX> malloc_local;
typedef ACE_Malloc<ACE_LOCAL_MEMORY_POOL, ACE_SYNCH_MUTEX> malloc_local_mutex;
typedef ACE_Malloc<ACE_SHARED_MEMORY_POOL, ACE_SYNCH_MUTEX> malloc_share_mutex;
typedef ACE_Malloc<ACE_SBRK_MEMORY_POOL, ACE_SYNCH_MUTEX> malloc_sbrk_mutex;
typedef ACE_Malloc<ACE_LITE_MMAP_MEMORY_POOL, ACE_SYNCH_MUTEX> malloc_mmap_lite;
 
template<class T>
void testmalloc(T &ml)
{
    int i = 0;
    for (i = 0; i < 1000; i++)
    {
        p[i] = ml.malloc(MAXSIZE);
        assert(p[i] != NULL);                                                                   
    }
   
    for (i = 0; i < 1000; i++)
    {
         ml.free(p[i]);
         p[i] = NULL;
    }
}
 
int main(int argc,char* argv[])
{
 
    BEGIN(10000)
        testmalloc();
    END("testmalloc")
 
    BEGIN(10000)
        testcached();
    END("testcached")
 
    ACE_MMAP_Memory_Pool_Options options(ACE_DEFAULT_BASE_ADDR, ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED, 1024 * 10000 );
    malloc_mmap mm("hello", "hello", &options);
    //fprintf(stderr, "%d chuncks/n", ml.avail_chunks(MAXSIZE));
    BEGIN(10000)
        testmalloc(mm);
    END("testmallocmmap")
    mm.remove();
 
     malloc_mmap_lite mml("helloq", "helloq", &options);
    //fprintf(stderr, "%d chuncks/n", ml.avail_chunks(MAXSIZE));
    BEGIN(10000)
        testmalloc(mml);
    END("testmallocmmaplite")
    mml.remove();
 
    malloc_local ml("hello1");
    //fprintf(stderr, "%d chuncks/n", ml.avail_chunks(MAXSIZE));
    BEGIN(10000)
        testmalloc(ml);
    END("testmalloclocal")
    ml.remove();
 
    malloc_local_mutex mlm("hello2");
    //fprintf(stderr, "%d chuncks/n", ml.avail_chunks(MAXSIZE));
    BEGIN(10000)
        testmalloc(mlm);
    END("testmalloclocalmutex")
    mlm.remove();
 
    malloc_local mlb("hellob");
    //fprintf(stderr, "%d chuncks/n", ml.avail_chunks(MAXSIZE));
    BEGIN(10000)
        testmalloc(mlb);
    END("testmalloc_brk_mutex")
    mlb.remove();
   
    ACE_Shared_Memory_Pool_Options options2(ACE_DEFAULT_BASE_ADDR, 1, ACE_DEFAULT_FILE_PERMS, 1,  1024 * 1200 );
    malloc_share_mutex msm("hello3", "hello3",&options2);
    BEGIN(10000)
        testmalloc(msm);
    END("testmallocsharemutex")
    msm.remove();
           
    return 0;
}
 
原创粉丝点击