bench_alloc_expand_bwd.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifdef _MSC_VER
  11. #pragma warning (disable : 4512)
  12. #endif
  13. #include <boost/container/allocator.hpp>
  14. #define BOOST_CONTAINER_VECTOR_ALLOC_STATS
  15. #include <boost/container/vector.hpp>
  16. #include <memory> //std::allocator
  17. #include <iostream> //std::cout, std::endl
  18. #include <cassert> //assert
  19. #include <boost/timer/timer.hpp>
  20. using boost::timer::cpu_timer;
  21. using boost::timer::cpu_times;
  22. using boost::timer::nanosecond_type;
  23. namespace bc = boost::container;
  24. typedef std::allocator<int> StdAllocator;
  25. typedef bc::allocator<int, 2, bc::expand_bwd | bc::expand_fwd> AllocatorPlusV2Mask;
  26. typedef bc::allocator<int, 2, bc::expand_fwd> AllocatorPlusV2;
  27. typedef bc::allocator<int, 1> AllocatorPlusV1;
  28. template<class Allocator> struct get_allocator_name;
  29. template<> struct get_allocator_name<StdAllocator>
  30. { static const char *get() { return "StdAllocator"; } };
  31. template<> struct get_allocator_name<AllocatorPlusV2Mask>
  32. { static const char *get() { return "AllocatorPlusV2Mask"; } };
  33. template<> struct get_allocator_name<AllocatorPlusV2>
  34. { static const char *get() { return "AllocatorPlusV2"; } };
  35. template<> struct get_allocator_name<AllocatorPlusV1>
  36. { static const char *get() { return "AllocatorPlusV1"; } };
  37. //typedef int MyInt;
  38. class MyInt
  39. {
  40. int int_;
  41. public:
  42. MyInt(int i = 0)
  43. : int_(i)
  44. {}
  45. MyInt(const MyInt &other)
  46. : int_(other.int_)
  47. {}
  48. MyInt & operator=(const MyInt &other)
  49. {
  50. int_ = other.int_;
  51. return *this;
  52. }
  53. ~MyInt()
  54. {
  55. int_ = 0;
  56. }
  57. };
  58. namespace boost{
  59. template<class T>
  60. struct has_trivial_destructor_after_move;
  61. template<>
  62. struct has_trivial_destructor_after_move<MyInt>
  63. {
  64. static const bool value = true;
  65. //static const bool value = false;
  66. };
  67. } //namespace boost{
  68. void print_header()
  69. {
  70. std::cout << "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
  71. << "Capacity" << ";" << "push_back(ns)" << ";" << "Allocator calls" << ";"
  72. << "New allocations" << ";" << "Bwd expansions" << std::endl;
  73. }
  74. template<class Allocator>
  75. void vector_test_template(unsigned int num_iterations, unsigned int num_elements, bool csv_output)
  76. {
  77. typedef typename Allocator::template rebind<MyInt>::other IntAllocator;
  78. unsigned int numalloc = 0, numexpand = 0;
  79. cpu_timer timer;
  80. timer.resume();
  81. unsigned int capacity = 0;
  82. for(unsigned int r = 0; r != num_iterations; ++r){
  83. bc::vector<MyInt, IntAllocator> v;
  84. v.reset_alloc_stats();
  85. void *first_mem = 0;
  86. try{
  87. first_mem = bc::dlmalloc_malloc(sizeof(MyInt)*num_elements*3/2);
  88. v.push_back(MyInt(0));
  89. bc::dlmalloc_free(first_mem);
  90. for(unsigned int e = 0; e != num_elements; ++e){
  91. v.push_back(MyInt(e));
  92. }
  93. numalloc += v.num_alloc;
  94. numexpand += v.num_expand_bwd;
  95. capacity = static_cast<unsigned int>(v.capacity());
  96. }
  97. catch(...){
  98. bc::dlmalloc_free(first_mem);
  99. throw;
  100. }
  101. }
  102. assert(bc::dlmalloc_allocated_memory() == 0);
  103. timer.stop();
  104. nanosecond_type nseconds = timer.elapsed().wall;
  105. if(csv_output){
  106. std::cout << get_allocator_name<Allocator>::get()
  107. << ";"
  108. << num_iterations
  109. << ";"
  110. << num_elements
  111. << ";"
  112. << capacity
  113. << ";"
  114. << float(nseconds)/(num_iterations*num_elements)
  115. << ";"
  116. << (float(numalloc) + float(numexpand))/num_iterations
  117. << ";"
  118. << float(numalloc)/num_iterations
  119. << ";"
  120. << float(numexpand)/num_iterations
  121. << std::endl;
  122. }
  123. else{
  124. std::cout << std::endl
  125. << "Allocator: " << get_allocator_name<Allocator>::get()
  126. << std::endl
  127. << " push_back ns: "
  128. << float(nseconds)/(num_iterations*num_elements)
  129. << std::endl
  130. << " capacity - alloc calls (new/expand): "
  131. << (unsigned int)capacity << " - "
  132. << (float(numalloc) + float(numexpand))/num_iterations
  133. << "(" << float(numalloc)/num_iterations << "/" << float(numexpand)/num_iterations << ")"
  134. << std::endl;
  135. std::cout << '\n'
  136. << " ----------------------------------- "
  137. << std::endl;
  138. }
  139. bc::dlmalloc_trim(0);
  140. }
  141. int main(int argc, const char *argv[])
  142. {
  143. //#define SINGLE_TEST
  144. #define SIMPLE_IT
  145. #ifdef SINGLE_TEST
  146. #ifdef NDEBUG
  147. unsigned int numit [] = { 10 };
  148. #else
  149. unsigned int numit [] = { 10 };
  150. #endif
  151. unsigned int numele [] = { 10000 };
  152. #elif defined(SIMPLE_IT)
  153. unsigned int numit [] = { 3 };
  154. unsigned int numele[] = { 10000 };
  155. #else
  156. #ifdef NDEBUG
  157. unsigned int numit [] = { 2000, 20000, 200000, 2000000 };
  158. #else
  159. unsigned int numit [] = { 100, 1000, 10000, 100000 };
  160. #endif
  161. unsigned int numele [] = { 10000, 1000, 100, 10 };
  162. #endif
  163. bool csv_output = argc == 2 && (strcmp(argv[1], "--csv-output") == 0);
  164. if(csv_output){
  165. print_header();
  166. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  167. vector_test_template<StdAllocator>(numit[i], numele[i], csv_output);
  168. }
  169. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  170. vector_test_template<AllocatorPlusV1>(numit[i], numele[i], csv_output);
  171. }
  172. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  173. vector_test_template<AllocatorPlusV2Mask>(numit[i], numele[i], csv_output);
  174. }
  175. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  176. vector_test_template<AllocatorPlusV2>(numit[i], numele[i], csv_output);
  177. }
  178. }
  179. else{
  180. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  181. std::cout << "\n ----------------------------------- \n"
  182. << " Iterations/Elements: " << numit[i] << "/" << numele[i]
  183. << "\n ----------------------------------- \n";
  184. vector_test_template<StdAllocator>(numit[i], numele[i], csv_output);
  185. vector_test_template<AllocatorPlusV1>(numit[i], numele[i], csv_output);
  186. vector_test_template<AllocatorPlusV2Mask>(numit[i], numele[i], csv_output);
  187. vector_test_template<AllocatorPlusV2>(numit[i], numele[i], csv_output);
  188. }
  189. }
  190. return 0;
  191. }