您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 资本运营 > 一个测试ACE各种内存分配策略效率的程序
一个测试ACE各种内存分配策略效率的程序64位平台C/C++开发注意事项在/en/l/上例出了28个在64位平台上使用C/C++开发的注意事项,对于进入64位时代的程序员应该去看看这28个事项,这些英文读物对于有C/C++功底的朋友读起来应该并不难,我估计大约20-30分钟可以精读完一篇(或者更快),下面是这28个注意事项的列表。相信对大家一点有帮助。Bhui2014(n)_begin=clock();for(_loop=0;_loop_loop++){#defineEND(s)}_end=clock();printf(%sloop%dusetime%lfs,s,_loop,(double)(_end-_begin)/(double)CLOCKS_PER_SEC);constintMAXSIZE=1024;void*p[1000];voidtestmalloc(){inti=0;for(i=0;i1000;i++){p[i]=malloc(MAXSIZE);assert(p[i]!=NULL);}for(i=0;i1000;i++){free(p[i]);}}typedefcharBLOCK[MAXSIZE];typedefACE_Dynamic_Cached_AllocatorACE_SYNCH_NULL_MUTEXAllocator;Allocatorcache(1000,MAXSIZE);voidtestcached(){inti=0;for(i=0;i1000;i++){p[i]=cache.malloc(MAXSIZE);assert(p[i]!=NULL);}for(i=0;i1000;i++){cache.free(p[i]);}}typedefACE_MallocACE_MMAP_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_mmap;typedefACE_MallocACE_LOCAL_MEMORY_POOL,ACE_SYNCH_NULL_MUTEXmalloc_local;typedefACE_MallocACE_LOCAL_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_local_mutex;typedefACE_MallocACE_SHARED_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_share_mutex;typedefACE_MallocACE_SBRK_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_sbrk_mutex;typedefACE_MallocACE_LITE_MMAP_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_mmap_lite;templateclassTvoidtestmalloc(Tml){inti=0;for(i=0;i1000;i++){p[i]=ml.malloc(MAXSIZE);assert(p[i]!=NULL);}for(i=0;i1000;i++){ml.free(p[i]);p[i]=NULL;}}intmain(intargc,char*argv[]){BEGIN(10000)testmalloc();END(testmalloc)BEGIN(10000)testcached();END(testcached)ACE_MMAP_Memory_Pool_Optionsoptions(ACE_DEFAULT_BASE_ADDR,ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED,1024*10000);malloc_mmapmm(hello,hello,options);//fprintf(stderr,%dchuncks,ml.avail_chunks(MAXSIZE));BEGIN(10000)testmalloc(mm);END(testmallocmmap)mm.remove();malloc_mmap_litemml(helloq,helloq,options);//fprintf(stderr,%dchuncks,ml.avail_chunks(MAXSIZE));BEGIN(10000)testmalloc(mml);END(testmallocmmaplite)mml.remove();malloc_localml(hello1//fprintf(stderr,%dchuncks,ml.avail_chunks(MAXSIZE));BEGIN(10000)testmalloc(ml);END(testmalloclocal)ml.remove();malloc_local_mutexmlm(hello2//fprintf(stderr,%dchuncks,ml.avail_chunks(MAXSIZE));BEGIN(10000)testmalloc(mlm);END(testmalloclocalmutex)mlm.remove();malloc_localmlb(hellob//fprintf(stderr,%dchuncks,ml.avail_chunks(MAXSIZE));BEGIN(10000)testmalloc(mlb);END(testmalloc_brk_mutex)mlb.remove();ACE_Shared_Memory_Pool_Optionsoptions2(ACE_DEFAULT_BASE_ADDR,1,ACE_DEFAULT_FILE_PERMS,1,1024*1200);malloc_share_mutexmsm(hello3,hello3,options2);BEGIN(10000)testmalloc(msm);END(testmallocsharemutex)msm.remove();return0;}来自:/blog/cns!F66D6083FD44CD13!183.entrytestmallocloop10000usetime32.950000stestcachedloop10000usetime1.730000stestmallocmmaploop10000usetime5.050000stestmallocmmapliteloop10000usetime5.030000stestmalloclocalloop10000usetime0.620000stestmalloclocalmutexloop10000usetime4.250000stestmalloc_brk_mutexloop10000usetime0.600000stestmallocsharemutexloop10000usetime4.360000s#includeace/OS_NS_stdio.h#includeace/OS_NS_string.h#includeace/Malloc_T.h#includeace/Null_Mutex.h#includetime.h#includeassert.hclock_t_begin;clock_t_end;int_loop;#defineBEGIN(n)_begin=clock();for(_loop=0;_loop_loop++){#defineEND(s)}_end=clock();printf(%sloop%dusetime%lfs,s,_loop,(double)(_end-_begin)/(double)CLOCKS_PER_SEC);constintMAXSIZE=1024;void*p[1000];voidtestmalloc(){inti=0;for(i=0;i1000;i++){p[i]=malloc(MAXSIZE);assert(p[i]!=NULL);}for(i=0;i1000;i++){free(p[i]);}}typedefcharBLOCK[MAXSIZE];typedefACE_Dynamic_Cached_AllocatorACE_SYNCH_NULL_MUTEXAllocator;Allocatorcache(1000,MAXSIZE);voidtestcached(){inti=0;for(i=0;i1000;i++){p[i]=cache.malloc(MAXSIZE);assert(p[i]!=NULL);}for(i=0;i1000;i++){cache.free(p[i]);}}typedefACE_MallocACE_MMAP_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_mmap;typedefACE_MallocACE_LOCAL_MEMORY_POOL,ACE_SYNCH_NULL_MUTEXmalloc_local;typedefACE_MallocACE_LOCAL_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_local_mutex;typedefACE_MallocACE_SHARED_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_share_mutex;typedefACE_MallocACE_SBRK_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_sbrk_mutex;typedefACE_MallocACE_LITE_MMAP_MEMORY_POOL,ACE_SYNCH_MUTEXmalloc_mmap_lite;templateclassTvoidtestmalloc(Tml){inti=0;for(i=0;i1000;i++){p[i]=ml.malloc(MAXSIZE);assert(p[i]!=NULL);}for(i=0;i1000;i++){ml.free(p[i]);p[i]=NULL;}}intmain(intargc,char*argv[]){BEGIN(10000)testmalloc();END(testmalloc)BEGIN(10000)testcached();END(testcached)ACE_MMAP_Memory_Pool_Optionsoptions(ACE_DEFAULT_BASE_ADDR,ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED,1024*10000);malloc_mmapmm(hello,hello,options);//fprintf(stderr,%dchuncks,ml.avail_
本文标题:一个测试ACE各种内存分配策略效率的程序
链接地址:https://www.777doc.com/doc-801502 .html