以STL 的运用角度而言,空间配置器是最不需要介绍的东西,它总是隐藏在一切组件(更具体地说是指容器,container)的背后,默默工作默默付出。
一、分配器测试
测试代码
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 10 #include <cstddef> 11 #include <memory> //内含 std::allocator 12 //欲使用 std::allocator 以外的 allocator, 得自行 #include <ext\...> 13 #ifdef __GNUC__ 14 #include <ext\array_allocator.h> 15 #include <ext\mt_allocator.h> 16 #include <ext\debug_allocator.h> 17 #include <ext\pool_allocator.h> 18 #include <ext\bitmap_allocator.h> 19 #include <ext\malloc_allocator.h> 20 #include <ext\new_allocator.h> 21 #endif 22 23 namespace jj20 24 { 25 //pass A object to function template impl(), 26 //而 A 本身是个 class template, 带有 type parameter T, 27 //那么有无可能在 impl() 中抓出 T, 创建一个 list<T, A<T>> object? 28 //以下先暂时回避上述疑问. 29 30 void test_list_with_special_allocator() 31 { 32 #ifdef __GNUC__ 33 cout << "\ntest_list_with_special_allocator().......... \n"; 34 35 //不能在 switch case 中宣告,只好下面这样. //1000000次 36 list<string, allocator<string>> c1; //3140 37 list<string, __gnu_cxx::malloc_allocator<string>> c2; //3110 38 list<string, __gnu_cxx::new_allocator<string>> c3; //3156 39 list<string, __gnu_cxx::__pool_alloc<string>> c4; //4922 40 list<string, __gnu_cxx::__mt_alloc<string>> c5; //3297 41 list<string, __gnu_cxx::bitmap_allocator<string>> c6; //4781 42 43 int choice; 44 long value; 45 46 cout << "select: " 47 << " (1) std::allocator " 48 << " (2) malloc_allocator " 49 << " (3) new_allocator " 50 << " (4) __pool_alloc " 51 << " (5) __mt_alloc " 52 << " (6) bitmap_allocator "; 53 54 cin >> choice; 55 if (choice != 0) { 56 cout << "how many elements: "; 57 cin >> value; 58 } 59 60 char buf[10]; 61 clock_t timeStart = clock(); 62 for (long i = 0; i< value; ++i) 63 { 64 try { 65 snprintf(buf, 10, "%d", i); 66 switch (choice) 67 { 68 case 1: c1.push_back(string(buf)); 69 break; 70 case 2: c2.push_back(string(buf)); 71 break; 72 case 3: c3.push_back(string(buf)); 73 break; 74 case 4: c4.push_back(string(buf)); 75 break; 76 case 5: c5.push_back(string(buf)); 77 break; 78 case 6: c6.push_back(string(buf)); 79 break; 80 default: 81 break; 82 } 83 } 84 catch (exception& p) { 85 cout << "i=" << i << " " << p.what() << endl; 86 abort(); 87 } 88 } 89 cout << "a lot of push_back(), milli-seconds : " << (clock() - timeStart) << endl; 90 91 92 //test all allocators' allocate() & deallocate(); 93 int* p; 94 allocator<int> alloc1; 95 p = alloc1.allocate(1); 96 alloc1.deallocate(p, 1); 97 98 __gnu_cxx::malloc_allocator<int> alloc2; 99 p = alloc2.allocate(1); 100 alloc2.deallocate(p, 1); 101 102 __gnu_cxx::new_allocator<int> alloc3; 103 p = alloc3.allocate(1); 104 alloc3.deallocate(p, 1); 105 106 __gnu_cxx::__pool_alloc<int> alloc4; 107 p = alloc4.allocate(2); 108 alloc4.deallocate(p, 2); //我刻意令参数为 2, 但这有何意义!! 一次要 2 个 ints? 109 110 __gnu_cxx::__mt_alloc<int> alloc5; 111 p = alloc5.allocate(1); 112 alloc5.deallocate(p, 1); 113 114 __gnu_cxx::bitmap_allocator<int> alloc6; 115 p = alloc6.allocate(3); 116 alloc6.deallocate(p, 3); //我刻意令参数为 3, 但这有何意义!! 一次要 3 个 ints? 117 #endif 118 } 119 }