bench_alloc_expand_fwd.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #pragma warning (disable : 4267)
  13. #pragma warning (disable : 4244)
  14. #endif
  15. #define BOOST_CONTAINER_VECTOR_ALLOC_STATS
  16. #include <boost/container/allocator.hpp>
  17. #include <vector>
  18. #include <boost/container/vector.hpp>
  19. #include <memory> //std::allocator
  20. #include <iostream> //std::cout, std::endl
  21. #include <cstring> //std::strcmp
  22. #include <boost/timer/timer.hpp>
  23. #include <typeinfo>
  24. using boost::timer::cpu_timer;
  25. using boost::timer::cpu_times;
  26. using boost::timer::nanosecond_type;
  27. namespace bc = boost::container;
  28. #if defined(BOOST_CONTAINER_VECTOR_ALLOC_STATS)
  29. template<class T, class Allocator>
  30. static void reset_alloc_stats(std::vector<T, Allocator> &)
  31. {}
  32. template<class T, class Allocator>
  33. static std::size_t get_num_alloc(std::vector<T, Allocator> &)
  34. { return 0; }
  35. template<class T, class Allocator>
  36. static std::size_t get_num_expand(std::vector<T, Allocator> &)
  37. { return 0; }
  38. template<class T, class Allocator>
  39. static void reset_alloc_stats(bc::vector<T, Allocator> &v)
  40. { v.reset_alloc_stats(); }
  41. template<class T, class Allocator>
  42. static std::size_t get_num_alloc(bc::vector<T, Allocator> &v)
  43. { return v.num_alloc; }
  44. template<class T, class Allocator>
  45. static std::size_t get_num_expand(bc::vector<T, Allocator> &v)
  46. { return v.num_expand_fwd; }
  47. #endif //BOOST_CONTAINER_VECTOR_ALLOC_STATS
  48. class MyInt
  49. {
  50. int int_;
  51. public:
  52. explicit MyInt(int i = 0)
  53. : int_(i)
  54. {}
  55. MyInt(const MyInt &other)
  56. : int_(other.int_)
  57. {}
  58. MyInt & operator=(const MyInt &other)
  59. {
  60. int_ = other.int_;
  61. return *this;
  62. }
  63. ~MyInt()
  64. {
  65. int_ = 0;
  66. }
  67. };
  68. template<class Container>
  69. void vector_test_template(unsigned int num_iterations, unsigned int num_elements)
  70. {
  71. unsigned int numalloc = 0, numexpand = 0;
  72. cpu_timer timer;
  73. timer.resume();
  74. unsigned int capacity = 0;
  75. for(unsigned int r = 0; r != num_iterations; ++r){
  76. Container v;
  77. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  78. reset_alloc_stats(v);
  79. #endif
  80. //v.reserve(num_elements);
  81. //MyInt a[3];
  82. /*
  83. for(unsigned int e = 0; e != num_elements/3; ++e){
  84. v.insert(v.end(), &a[0], &a[0]+3);
  85. }*/
  86. /*
  87. for(unsigned int e = 0; e != num_elements/3; ++e){
  88. v.insert(v.end(), 3, MyInt(e));
  89. }*/
  90. /*
  91. for(unsigned int e = 0; e != num_elements/3; ++e){
  92. v.insert(v.empty() ? v.end() : --v.end(), &a[0], &a[0]+3);
  93. }*/
  94. /*
  95. for(unsigned int e = 0; e != num_elements/3; ++e){
  96. v.insert(v.empty() ? v.end() : --v.end(), 3, MyInt(e));
  97. }*/
  98. /*
  99. for(unsigned int e = 0; e != num_elements/3; ++e){
  100. v.insert(v.size() >= 3 ? v.end()-3 : v.begin(), &a[0], &a[0]+3);
  101. }*/
  102. /*
  103. for(unsigned int e = 0; e != num_elements/3; ++e){
  104. v.insert(v.size() >= 3 ? v.end()-3 : v.begin(), 3, MyInt(e));
  105. }*/
  106. /*
  107. for(unsigned int e = 0; e != num_elements; ++e){
  108. v.insert(v.end(), MyInt(e));
  109. }*/
  110. /*
  111. for(unsigned int e = 0; e != num_elements; ++e){
  112. v.insert(v.empty() ? v.end() : --v.end(), MyInt(e));
  113. }*/
  114. for(unsigned int e = 0; e != num_elements; ++e){
  115. v.push_back(MyInt(e));
  116. }
  117. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  118. numalloc += get_num_alloc(v);
  119. numexpand += get_num_expand(v);
  120. #endif
  121. capacity = static_cast<unsigned int>(v.capacity());
  122. }
  123. timer.stop();
  124. nanosecond_type nseconds = timer.elapsed().wall;
  125. std::cout << std::endl
  126. << "Allocator: " << typeid(typename Container::allocator_type).name()
  127. << std::endl
  128. << " push_back ns: "
  129. << float(nseconds)/(num_iterations*num_elements)
  130. << std::endl
  131. << " capacity - alloc calls (new/expand): "
  132. << (unsigned int)capacity << " - "
  133. << (float(numalloc) + float(numexpand))/num_iterations
  134. << "(" << float(numalloc)/num_iterations << "/" << float(numexpand)/num_iterations << ")"
  135. << std::endl << std::endl;
  136. bc::dlmalloc_trim(0);
  137. }
  138. void print_header()
  139. {
  140. std::cout << "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
  141. << "Capacity" << ";" << "push_back(ns)" << ";" << "Allocator calls" << ";"
  142. << "New allocations" << ";" << "Fwd expansions" << std::endl;
  143. }
  144. int main()
  145. {
  146. //#define SINGLE_TEST
  147. #define SIMPLE_IT
  148. #ifdef SINGLE_TEST
  149. #ifdef NDEBUG
  150. std::size_t numit [] = { 1000 };
  151. #else
  152. std::size_t numit [] = { 100 };
  153. #endif
  154. std::size_t numele [] = { 10000 };
  155. #elif defined SIMPLE_IT
  156. std::size_t numit [] = { 3 };
  157. std::size_t numele [] = { 10000 };
  158. #else
  159. #ifdef NDEBUG
  160. unsigned int numit [] = { 1000, 10000, 100000, 1000000 };
  161. #else
  162. unsigned int numit [] = { 100, 1000, 10000, 100000 };
  163. #endif
  164. unsigned int numele [] = { 10000, 1000, 100, 10 };
  165. #endif
  166. print_header();
  167. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  168. vector_test_template< bc::vector<MyInt, std::allocator<MyInt> > >(numit[i], numele[i]);
  169. vector_test_template< bc::vector<MyInt, bc::allocator<MyInt, 1> > >(numit[i], numele[i]);
  170. vector_test_template<bc::vector<MyInt, bc::allocator<MyInt, 2, bc::expand_bwd | bc::expand_fwd> > >(numit[i], numele[i]);
  171. vector_test_template<bc::vector<MyInt, bc::allocator<MyInt, 2> > >(numit[i], numele[i]);
  172. }
  173. return 0;
  174. }