第七节:分配器测试

  • 标准的分配器Allocator,#include<ext/...>都是拓展的
  • 可以用不同的分配器测试同一容器
  • 分配器allocate() & deallocate()进行内存的分配和释放,这样操作太麻烦了
  • 实际用到得new delete malloc free释放时并没有指定分配产生的字节
  • 关于分配器实现需要后续的源码分析

  • 测试
  1. #include <list>
  2. #include <stdexcept>
  3. #include <string>
  4. #include <cstdlib> //abort()
  5. #include <cstdio> //snprintf()
  6. #include <algorithm> //find()
  7. #include <iostream>
  8. #include <ctime>
  9. #include <cstddef>
  10. #include <memory> //內含 std::allocator
  11. //欲使用 std::allocator 以外的 allocator, 得自行 #include <ext\...>
  12. #ifdef __GNUC__
  13. #include <ext\array_allocator.h>
  14. #include <ext\mt_allocator.h>
  15. #include <ext\debug_allocator.h>
  16. #include <ext\pool_allocator.h>
  17. #include <ext\bitmap_allocator.h>
  18. #include <ext\malloc_allocator.h>
  19. #include <ext\new_allocator.h>
  20. #endif
  21. namespace jj20
  22. {
  23. //pass A object to function template impl(),
  24. //而 A 本身是個 class template, 帶有 type parameter T,
  25. //那麼有無可能在 impl() 中抓出 T, 創建一個 list<T, A<T>> object?
  26. //以下先暫時迴避上述疑問.
  27. void test_list_with_special_allocator()
  28. {
  29. #ifdef __GNUC__
  30. cout << "\ntest_list_with_special_allocator().......... \n";
  31. //不能在 switch case 中宣告,只好下面這樣. //1000000次
  32. list<string, allocator<string>> c1; //3140
  33. list<string, __gnu_cxx::malloc_allocator<string>> c2; //3110
  34. list<string, __gnu_cxx::new_allocator<string>> c3; //3156
  35. list<string, __gnu_cxx::__pool_alloc<string>> c4; //4922
  36. list<string, __gnu_cxx::__mt_alloc<string>> c5; //3297
  37. list<string, __gnu_cxx::bitmap_allocator<string>> c6; //4781
  38. int choice;
  39. long value;
  40. cout << "select: "
  41. << " (1) std::allocator "
  42. << " (2) malloc_allocator "
  43. << " (3) new_allocator "
  44. << " (4) __pool_alloc "
  45. << " (5) __mt_alloc "
  46. << " (6) bitmap_allocator ";
  47. cin >> choice;
  48. if ( choice != 0 ) {
  49. cout << "how many elements: ";
  50. cin >> value;
  51. }
  52. char buf[10];
  53. clock_t timeStart = clock();
  54. for(long i=0; i< value; ++i)
  55. {
  56. try {
  57. snprintf(buf, 10, "%d", i);
  58. switch (choice)
  59. {
  60. case 1 : c1.push_back(string(buf));
  61. break;
  62. case 2 : c2.push_back(string(buf));
  63. break;
  64. case 3 : c3.push_back(string(buf));
  65. break;
  66. case 4 : c4.push_back(string(buf));
  67. break;
  68. case 5 : c5.push_back(string(buf));
  69. break;
  70. case 6 : c6.push_back(string(buf));
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. catch(exception& p) {
  77. cout << "i=" << i << " " << p.what() << endl;
  78. abort();
  79. }
  80. }
  81. cout << "a lot of push_back(), milli-seconds : " << (clock()-timeStart) << endl;
  82. //test all allocators' allocate() & deallocate();
  83. int* p;
  84. allocator<int> alloc1;
  85. p = alloc1.allocate(1);
  86. alloc1.deallocate(p,1);
  87. __gnu_cxx::malloc_allocator<int> alloc2;
  88. p = alloc2.allocate(1);
  89. alloc2.deallocate(p,1);
  90. __gnu_cxx::new_allocator<int> alloc3;
  91. p = alloc3.allocate(1);
  92. alloc3.deallocate(p,1);
  93. __gnu_cxx::__pool_alloc<int> alloc4;
  94. p = alloc4.allocate(2);
  95. alloc4.deallocate(p,2); //我刻意令參數為 2, 但這有何意義!! 一次要 2 個 ints?
  96. __gnu_cxx::__mt_alloc<int> alloc5;
  97. p = alloc5.allocate(1);
  98. alloc5.deallocate(p,1);
  99. __gnu_cxx::bitmap_allocator<int> alloc6;
  100. p = alloc6.allocate(3);
  101. alloc6.deallocate(p,3); //我刻意令參數為 3, 但這有何意義!! 一次要 3 個 ints?
  102. #endif
  103. }
  104. }

侯捷STL学习(三)--分配器测试的更多相关文章

  1. 侯捷STL学习(12)--STL相关内容hash+tuple

    layout: post title: 侯捷STL学习(12) date: 2017-08-01 tag: 侯捷STL --- 第四讲 STL相关的内容 Hash Function 将hash函数封装 ...

  2. 侯捷STL学习(11)--算仿+仿函数+适配器

    layout: post title: 侯捷STL学习(十一) date: 2017-07-24 tag: 侯捷STL --- 第三讲 标准库内核分析-算法 标准库算法形式 iterator分类 不同 ...

  3. 侯捷STL学习(十)--容器hashtable探索(unordered set/map)

    layout: post title: 侯捷STL学习(十) date: 2017-07-23 tag: 侯捷STL --- 第二十三节 容器hashtable探索 hashtable冲突(碰撞)处理 ...

  4. 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)

    layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ...

  5. 侯捷STL学习(八)-- 深度探索deque

    layout: post title: 侯捷STL学习(八) date: 2017-07-19 tag: 侯捷STL --- 第十八节 深度探索deque上 duque内存结构 分段连续,用户看起来是 ...

  6. 侯捷STL学习(七)--深度探索vector&&array

    layout: post title: 侯捷STL学习(七) date: 2017-06-13 tag: 侯捷STL --- 第十六节 深度探索vector vector源码剖析 vector内存2倍 ...

  7. 侯捷STL学习(一)--顺序容器测试

    开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ...

  8. 侯捷STL学习(一)

    开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ...

  9. 侯捷STL学习(二)--序列容器测试

    第六节:容器之分类和各种测试(四) stack不提供iterator操作,破坏了容器的独特性,先进先出. 使用容器multiset(允许元素重复) 内部是红黑树,insert操作就保证了排好了序. 标 ...

随机推荐

  1. org.apache.flume.ChannelException: Take list for MemoryTransaction, capacity 100 full, consider committing more frequently, increasing capacity, or increasing thread count

    flume在抽取MySQL数据到kafka时报错,如下 [SinkRunner-PollingRunner-DefaultSinkProcessor] ERROR org.apache.flume.s ...

  2. windows系统JDK的安装及环境配置

    本文转载至:http://blog.csdn.net/sweetburden2011/article/details/8881181 一:JDK的安装 1.   首先上甲骨文公司的官方网站下载JDK的 ...

  3. MVC 中 System.Web.Optimization 找不到引用

    在MVC4的开发中,如果创建的项目为空MVC项目,那么在App_Start目录下没有BundleConfig.cs项的内容,在手动添加时在整个库中都找不到:System.Web.Optimizatio ...

  4. js适配器模式

    适配器模式,将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器,目的是使控制范 ...

  5. PHP中preg_match正则匹配的/u /i /s是什么意思

    /u 表示按unicode(utf-8)匹配(主要针对多字节比如汉字) /i 表示不区分大小写(如果表达式里面有 a, 那么 A 也是匹配对象) /s 表示将字符串视为单行来匹配

  6. 解析PHP中intval()等int转换时的意外异常情况

    <?php$a = 9.45*100;var_dump($a);var_dump(intval($a));$a = 945*1.00;var_dump($a);var_dump(intval($ ...

  7. 5.6 WebDriver API实例讲解(31-40)

    31.判断页面元素是否存在 public static void testElementExist(){ driver.get("http://www.sogou.com"); t ...

  8. 安装Nodejs、npm、Less(支持生成压缩后的css)

    安装Nodejs和npm 1.到https://nodejs.org/en/下载最新的版本,安装到d盘下,假设安装后的路径为D:\Program Files\nodejs, 笔者当前的版本v6.10. ...

  9. javascript queue 打字效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 【VS2013生成DirectX Tutorials时遇到的错误】无法解析的外部符号 _D3D10CreateDeviceAndSwapChain@32

     本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/3696472.html 今天尝试编译DirectX10中的一个Turorials时, ...