bench_alloc_shrink_to_fit.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #undef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  17. #include <memory> //std::allocator
  18. #include <iostream> //std::cout, std::endl
  19. #include <cassert> //assert
  20. #include <boost/timer/timer.hpp>
  21. using boost::timer::cpu_timer;
  22. using boost::timer::cpu_times;
  23. using boost::timer::nanosecond_type;
  24. namespace bc = boost::container;
  25. typedef std::allocator<int> StdAllocator;
  26. typedef bc::allocator<int, 2> 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<AllocatorPlusV2>
  32. { static const char *get() { return "AllocatorPlusV2"; } };
  33. template<> struct get_allocator_name<AllocatorPlusV1>
  34. { static const char *get() { return "AllocatorPlusV1"; } };
  35. class MyInt
  36. {
  37. std::size_t int_; //Use a type that will grow on 64 bit machines
  38. public:
  39. MyInt(int i = 0) : int_(i){}
  40. MyInt(const MyInt &other)
  41. : int_(other.int_)
  42. {}
  43. MyInt & operator=(const MyInt &other)
  44. {
  45. int_ = other.int_;
  46. return *this;
  47. }
  48. };
  49. void print_header()
  50. {
  51. std::cout << "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
  52. << "num_shrink" << ";" << "shrink_to_fit(ns)" << std::endl;
  53. }
  54. template<class Allocator>
  55. void vector_test_template(unsigned int num_iterations, unsigned int num_elements, bool csv_output)
  56. {
  57. typedef typename Allocator::template rebind<MyInt>::other IntAllocator;
  58. unsigned int capacity = 0;
  59. const std::size_t Step = 5;
  60. unsigned int num_shrink = 0;
  61. (void)capacity;
  62. cpu_timer timer;
  63. timer.resume();
  64. #ifndef NDEBUG
  65. typedef bc::dtl::integral_constant
  66. <unsigned, bc::dtl::version<Allocator>::value> alloc_version;
  67. #endif
  68. for(unsigned int r = 0; r != num_iterations; ++r){
  69. bc::vector<MyInt, IntAllocator> v(num_elements);
  70. v.reset_alloc_stats();
  71. num_shrink = 0;
  72. for(unsigned int e = num_elements; e != 0; e -= Step){
  73. v.erase(v.end() - Step, v.end());
  74. v.shrink_to_fit();
  75. assert( (alloc_version::value != 2) || (e == Step) || (v.num_shrink > num_shrink) );
  76. num_shrink = v.num_shrink;
  77. }
  78. assert(v.empty());
  79. assert(0 == v.capacity());
  80. }
  81. timer.stop();
  82. nanosecond_type nseconds = timer.elapsed().wall;
  83. if(csv_output){
  84. std::cout << get_allocator_name<Allocator>::get()
  85. << ";"
  86. << num_iterations
  87. << ";"
  88. << num_elements
  89. << ";"
  90. << num_shrink
  91. << ";"
  92. << float(nseconds)/(num_iterations*num_elements)
  93. << std::endl;
  94. }
  95. else{
  96. std::cout << std::endl
  97. << "Allocator: " << get_allocator_name<Allocator>::get()
  98. << std::endl
  99. << " num_shrink: " << num_shrink
  100. << std::endl
  101. << " shrink_to_fit ns: "
  102. << float(nseconds)/(num_iterations*num_elements)
  103. << std::endl << std::endl;
  104. }
  105. bc::dlmalloc_trim(0);
  106. }
  107. int main(int argc, const char *argv[])
  108. {
  109. //#define SINGLE_TEST
  110. #define SIMPLE_IT
  111. #ifdef SINGLE_TEST
  112. #ifdef NDEBUG
  113. unsigned int numit [] = { 10 };
  114. #else
  115. unsigned int numit [] = { 50 };
  116. unsigned int numele[] = { 2000 };
  117. #endif
  118. #elif defined SIMPLE_IT
  119. unsigned int numit [] = { 3 };
  120. unsigned int numele[] = { 2000 };
  121. #else
  122. #ifdef NDEBUG
  123. unsigned int numit [] = { 100, 1000, 10000 };
  124. #else
  125. unsigned int numit [] = { 10, 100, 1000 };
  126. #endif
  127. unsigned int numele [] = { 10000, 2000, 500 };
  128. #endif
  129. bool csv_output = argc == 2 && (strcmp(argv[1], "--csv-output") == 0);
  130. if(csv_output){
  131. print_header();
  132. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  133. vector_test_template<StdAllocator>(numit[i], numele[i], csv_output);
  134. }
  135. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  136. vector_test_template<AllocatorPlusV1>(numit[i], numele[i], csv_output);
  137. }
  138. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  139. vector_test_template<AllocatorPlusV2>(numit[i], numele[i], csv_output);
  140. }
  141. }
  142. else{
  143. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  144. std::cout << "\n ----------------------------------- \n"
  145. << " Iterations/Elements: " << numit[i] << "/" << numele[i]
  146. << "\n ----------------------------------- \n";
  147. vector_test_template<StdAllocator>(numit[i], numele[i], csv_output);
  148. vector_test_template<AllocatorPlusV1>(numit[i], numele[i], csv_output);
  149. vector_test_template<AllocatorPlusV2>(numit[i], numele[i], csv_output);
  150. }
  151. }
  152. return 0;
  153. }