一个测试ACE各种内存分配策略效率的程序_第1页
一个测试ACE各种内存分配策略效率的程序_第2页
一个测试ACE各种内存分配策略效率的程序_第3页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一个测试 ACE 各种内存分配策略效率的程 序64 位平台 C/C+开发注意事项 在/en/l/ 上例出了 28个在 64 位平台上使用 C/C+开发的注 意事项,对于进入 64 位时代的程序员应该去看看这 28个 事项,这些英文读物对于有 C/C+功底的朋友读起来应该 并不难,我估计大约 20-30 分钟可以精读完一篇(或者更 快),下面是这 28 个注意事项的列表。相信对大家一点 有帮助。 Lesson 01. What 64-bit systems are. Lesson 02. S testmalloc loop 10000 use time 32.950000 s testcache

2、d 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

3、 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 _loop+) #define END(s)_end = clock();pr

4、intf( %s loop %d use time %lf s , s, _loop , (double)(_end - _begin) / (double)CLOCKS_PER_SEC); const int MAXSIZE = 1024;void *p1000;void testmalloc()int i = 0;for (i = 0; i 1000; i+)pi = malloc(MAXSIZE);assert(pi != NULL);for (i = 0; i 1000; i+)free(pi);typedef char BLOCKMAXSIZE; typedef ACE_Dynami

5、c_Cached_Allocator ACE_SYNCH_NULL_MUTEX Allocator;Allocator cache(1000, MAXSIZE);void testcached()int i = 0;for (i = 0; i 1000; i+)pi = cache.malloc(MAXSIZE); assert(pi != NULL);for (i = 0; i 1000; i+) cache.free(pi);typedef ACE_Malloc ACE_MMAP_MEMORY_POOL, ACE_SYNCH_MUTEX malloc_mmap;typedef ACE_Ma

6、lloc 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

7、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+)pi = ml.malloc(MAXSIZE);assert(pi != NULL);for (i = 0; i 1000; i+)ml.free(pi);pi = NULL;int main(int argc,char* argv) BEGIN(10000) testmalloc();END( testmallo

8、c ) 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 , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mm);END( testmal

9、locmmap )mm.remove(); malloc_mmap_lite mml( helloq , helloq , options);/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mml);END( testmallocmmaplite )mml.remove(); malloc_local ml( hello1/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE);BEGIN(10000) testmalloc(ml);

10、END( testmalloclocal )ml.remove(); malloc_local_mutex mlm( hello2/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000) testmalloc(mlm);END( testmalloclocalmutex )mlm.remove(); malloc_local mlb( hellob/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000) testmalloc(mlb);

11、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;来自: /blog/cns!F66D60

12、83FD44CD13!183.entry 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

13、 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)_beg

14、in = clock();for (_loop = 0; _loop _loop+) #define END(s)_end = clock();printf( %s loop %d use time %lf s , s, _loop , (double)(_end - _begin) / (double)CLOCKS_PER_SEC); const int MAXSIZE = 1024;void *p1000;void testmalloc()int i = 0;for (i = 0; i 1000; i+)pi = malloc(MAXSIZE);assert(pi != NULL);for

15、 (i = 0; i 1000; i+)free(pi);typedef char BLOCKMAXSIZE;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+)pi = cache.malloc(MAXSIZE); assert(pi != NULL);for (i = 0; i 1000; i+) cache.free(pi);typedef AC

16、E_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 AC

17、E_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+)pi = ml.malloc(MAXSIZE);assert(pi != NULL);for (i = 0; i 1000; i+)ml.free(pi);pi = NU

18、LL;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 chun

19、cks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mm);END( testmallocmmap ) mm.remove(); malloc_mmap_lite mml( helloq , helloq , options);/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mml);END( testmallocmmaplite )mml.remove(); malloc_local ml( hello1/fprintf(st

20、derr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(ml);END( testmalloclocal )ml.remove(); malloc_local_mutex mlm( hello2 /fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE);BEGIN(10000)testmalloc(mlm);END( testmalloclocalmutex )mlm.remove(); malloc_local mlb( hellob/fprintf(stder

21、r, %d chuncks , 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

22、);END( testmallocsharemutex ) msm.remove();return 0;来自: /blog/cns!F66D6083FD44CD13!183.entry testmalloc loop 10000 use time 32.950000 stestcached 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

23、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 #inc

24、lude assert.h clock_t _begin; clock_t _end; int _loop;#define BEGIN(n) _begin = clock();for (_loop = 0; _loop _loop+) #define END(s)_end = clock();printf( %s loop %d use time %lf s , s, _loop , (double)(_end - _begin) / (double)CLOCKS_PER_SEC); const int MAXSIZE = 1024;void *p1000;void testmalloc()i

25、nt i = 0;for (i = 0; i 1000; i+)pi = malloc(MAXSIZE); assert(pi != NULL);for (i = 0; i 1000; i+) free(pi);typedef char BLOCKMAXSIZE;typedef ACE_Dynamic_Cached_AllocatorACE_SYNCH_NULL_MUTEX Allocator;Allocator cache(1000, MAXSIZE);void testcached()int i = 0;for (i = 0; i 1000; i+)pi = cache.malloc(MA

26、XSIZE);assert(pi != NULL);for (i = 0; i 1000; i+) cache.free(pi);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_M

27、alloc 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+)pi = ml.ma

28、lloc(MAXSIZE); assert(pi != NULL);for (i = 0; i 1000; i+)ml.free(pi);pi = 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,

29、1024 10000 );malloc_mmap mm( hello , hello , options);/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mm);END( testmallocmmap )mm.remove(); malloc_mmap_lite mml( helloq , helloq , options);/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(m

30、ml);END( testmallocmmaplite ) mml.remove(); malloc_local ml( hello1/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000) testmalloc(ml);END( testmalloclocal )ml.remove(); malloc_local_mutex mlm( hello2/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mlm);

31、END( testmalloclocalmutex )mlm.remove(); malloc_local mlb( hellob/fprintf(stderr, %d chuncks , 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 ); m

32、alloc_share_mutex msm( hello3 , hello3 , options2); BEGIN(10000) testmalloc(msm);END( testmallocsharemutex ) msm.remove();return 0;来自: /blog/cns!F66D6083FD44CD13!183.entry testmalloc loop 10000 use time 32.950000 s testcached loop 10000 use time 1.730000 s testmallocmmap loop 10000 use time 5.050000

33、 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 stestmallocsharemutex loop 10000 use time 4.360000 s#include ace/OS_NS_stdio.h#include ace/OS_NS_s

34、tring.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 _loop+) #define END(s)_end = clock();printf( %s loop %d use time %lf s , s, _loop , (double)(_end - _begin) / (double)

35、CLOCKS_PER_SEC); const int MAXSIZE = 1024;void *p1000;void testmalloc()int i = 0;for (i = 0; i 1000; i+)pi = malloc(MAXSIZE);assert(pi != NULL);for (i = 0; i 1000; i+) free(pi);typedef char BLOCKMAXSIZE;typedef ACE_Dynamic_Cached_AllocatorACE_SYNCH_NULL_MUTEX Allocator;Allocator cache(1000, MAXSIZE)

36、;void testcached()int i = 0;for (i = 0; i 1000; i+)pi = cache.malloc(MAXSIZE); assert(pi != NULL);for (i = 0; i 1000; i+)cache.free(pi);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

37、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 c

38、lass T void testmalloc(T ml)int i = 0;for (i = 0; i 1000; i+)pi = ml.malloc(MAXSIZE);assert(pi != NULL);for (i = 0; i 1000; i+) ml.free(pi); pi = NULL;int main(int argc,char* argv) BEGIN(10000) testmalloc();END( testmalloc ) BEGIN(10000) testcached();END( testcached ) ACE_MMAP_Memory_Pool_Options op

39、tions(ACE_DEFAULT_BASE_ADDR, ACE_MMAP_Memory_Pool_Options:ALWAYS_FIXED, 1024 10000 );malloc_mmap mm( hello , hello , options);/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mm);END( testmallocmmap ) mm.remove(); malloc_mmap_lite mml( helloq , helloq , options);/fprint

40、f(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mml);END( testmallocmmaplite )mml.remove(); malloc_local ml( hello1/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000) testmalloc(ml);END( testmalloclocal )ml.remove(); malloc_local_mutex mlm( hello2/fprintf(std

41、err, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000)testmalloc(mlm);END( testmalloclocalmutex )mlm.remove(); malloc_local mlb( hellob/fprintf(stderr, %d chuncks , ml.avail_chunks(MAXSIZE); BEGIN(10000) testmalloc(mlb);END( testmalloc_brk_mutex ) mlb.remove();ACE_Shared_Memory_Pool_Options option

42、s2(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;来自: /blog/cns!F66D6083FD44CD13!183.entry testmalloc loop 10000 use time 32.950000 s testcached loo

43、p 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

44、 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 _loop+) #define END(s)_end = clock();printf

45、( %s loop %d use time %lf s , s, _loop , (double)(_end - _begin) / (double)CLOCKS_PER_SEC); const int MAXSIZE = 1024;void *p1000;void testmalloc()int i = 0;for (i = 0; i 1000; i+)pi = malloc(MAXSIZE);assert(pi != NULL);for (i = 0; i 1000; i+)free(pi);typedef char BLOCKMAXSIZE;typedef ACE_Dynamic_Cac

46、hed_Allocator ACE_SYNCH_NULL_MUTEX Allocator;Allocator cache(1000, MAXSIZE);void testcached()int i = 0;for (i = 0; i 1000; i+)pi = cache.malloc(MAXSIZE); assert(pi != NULL);for (i = 0; i 1000; i+) cache.free(pi);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, A

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论