bench_alloc_stable_vector_burst.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 : 4541)
  13. #pragma warning (disable : 4673)
  14. #pragma warning (disable : 4671)
  15. #pragma warning (disable : 4244)
  16. #endif
  17. #include <memory> //std::allocator
  18. #include <iostream> //std::cout, std::endl
  19. #include <vector> //std::vector
  20. #include <cstddef> //std::size_t
  21. #include <cassert> //assert
  22. #include <boost/container/allocator.hpp>
  23. #include <boost/container/adaptive_pool.hpp>
  24. #include <boost/container/stable_vector.hpp>
  25. #include <boost/container/vector.hpp>
  26. #include <boost/timer/timer.hpp>
  27. using boost::timer::cpu_timer;
  28. using boost::timer::cpu_times;
  29. using boost::timer::nanosecond_type;
  30. namespace bc = boost::container;
  31. typedef std::allocator<int> StdAllocator;
  32. typedef bc::allocator<int, 1> AllocatorPlusV1;
  33. typedef bc::allocator<int, 2> AllocatorPlusV2;
  34. typedef bc::adaptive_pool
  35. < int
  36. , bc::ADP_nodes_per_block
  37. , 0//bc::ADP_max_free_blocks
  38. , 2
  39. , 2> AdPool2PercentV2;
  40. template<class Allocator> struct get_allocator_name;
  41. template<> struct get_allocator_name<StdAllocator>
  42. { static const char *get() { return "StdAllocator"; } };
  43. template<> struct get_allocator_name<AllocatorPlusV1>
  44. { static const char *get() { return "AllocatorPlusV1"; } };
  45. template<> struct get_allocator_name<AllocatorPlusV2>
  46. { static const char *get() { return "AllocatorPlusV2"; } };
  47. template<> struct get_allocator_name<AdPool2PercentV2>
  48. { static const char *get() { return "AdPool2PercentV2"; } };
  49. class MyInt
  50. {
  51. int int_;
  52. public:
  53. MyInt(int i = 0) : int_(i){}
  54. MyInt(const MyInt &other)
  55. : int_(other.int_)
  56. {}
  57. MyInt & operator=(const MyInt &other)
  58. {
  59. int_ = other.int_;
  60. return *this;
  61. }
  62. };
  63. template<class Allocator>
  64. struct get_vector
  65. {
  66. typedef bc::vector
  67. <MyInt, typename Allocator::template rebind<MyInt>::other> type;
  68. static const char *vector_name()
  69. {
  70. return "vector<MyInt>";
  71. }
  72. };
  73. template<class Allocator>
  74. struct get_stable_vector
  75. {
  76. typedef bc::stable_vector
  77. <MyInt, typename Allocator::template rebind<MyInt>::other> type;
  78. static const char *vector_name()
  79. {
  80. return "stable_vector<MyInt>";
  81. }
  82. };
  83. template<template<class> class GetContainer, class Allocator>
  84. void stable_vector_test_template(unsigned int num_iterations, unsigned int num_elements, bool csv_output)
  85. {
  86. typedef typename GetContainer<Allocator>::type vector_type;
  87. //std::size_t top_capacity = 0;
  88. nanosecond_type nseconds;
  89. {
  90. {
  91. vector_type l;
  92. cpu_timer timer;
  93. timer.resume();
  94. for(unsigned int r = 0; r != num_iterations; ++r){
  95. l.insert(l.end(), num_elements, MyInt(r));
  96. }
  97. timer.stop();
  98. nseconds = timer.elapsed().wall;
  99. if(csv_output){
  100. std::cout << get_allocator_name<Allocator>::get()
  101. << ";"
  102. << GetContainer<Allocator>::vector_name()
  103. << ";"
  104. << num_iterations
  105. << ";"
  106. << num_elements
  107. << ";"
  108. << float(nseconds)/(num_iterations*num_elements)
  109. << ";";
  110. }
  111. else{
  112. std::cout << "Allocator: " << get_allocator_name<Allocator>::get()
  113. << '\t'
  114. << GetContainer<Allocator>::vector_name()
  115. << std::endl
  116. << " allocation ns: "
  117. << float(nseconds)/(num_iterations*num_elements);
  118. }
  119. // top_capacity = l.capacity();
  120. //Now preprocess ranges to erase
  121. std::vector<typename vector_type::iterator> ranges_to_erase;
  122. ranges_to_erase.push_back(l.begin());
  123. for(unsigned int r = 0; r != num_iterations; ++r){
  124. typename vector_type::iterator next_pos(ranges_to_erase[r]);
  125. std::size_t n = num_elements;
  126. while(n--){ ++next_pos; }
  127. ranges_to_erase.push_back(next_pos);
  128. }
  129. //Measure range erasure function
  130. timer.stop();
  131. timer.start();
  132. for(unsigned int r = 0; r != num_iterations; ++r){
  133. std::size_t init_pos = (num_iterations-1)-r;
  134. l.erase(ranges_to_erase[init_pos], l.end());
  135. }
  136. timer.stop();
  137. nseconds = timer.elapsed().wall;
  138. assert(l.empty());
  139. }
  140. }
  141. if(csv_output){
  142. std::cout << float(nseconds)/(num_iterations*num_elements)
  143. << std::endl;
  144. }
  145. else{
  146. std::cout << '\t'
  147. << " deallocation ns: "
  148. << float(nseconds)/(num_iterations*num_elements)/*
  149. << std::endl
  150. << " max capacity: "
  151. << static_cast<unsigned int>(top_capacity)
  152. << std::endl
  153. << " remaining cap. "
  154. << static_cast<unsigned int>(top_capacity - num_iterations*num_elements)
  155. << " (" << (float(top_capacity)/float(num_iterations*num_elements) - 1)*100 << " %)"*/
  156. << std::endl << std::endl;
  157. }
  158. assert(bc::dlmalloc_all_deallocated());
  159. bc::dlmalloc_trim(0);
  160. }
  161. void print_header()
  162. {
  163. std::cout << "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
  164. << "Insertion time(ns)" << ";" << "Erasure time(ns)" << ";"
  165. << std::endl;
  166. }
  167. void stable_vector_operations()
  168. {
  169. {
  170. bc::stable_vector<int> a(bc::stable_vector<int>::size_type(5), 4);
  171. bc::stable_vector<int> b(a);
  172. bc::stable_vector<int> c(a.cbegin(), a.cend());
  173. b.insert(b.cend(), 0);
  174. c.pop_back();
  175. a.assign(b.cbegin(), b.cend());
  176. a.assign(c.cbegin(), c.cend());
  177. a.assign(1, 2);
  178. }
  179. {
  180. typedef bc::stable_vector<int, std::allocator<int> > stable_vector_t;
  181. stable_vector_t a(bc::stable_vector<int>::size_type(5), 4);
  182. stable_vector_t b(a);
  183. stable_vector_t c(a.cbegin(), a.cend());
  184. b.insert(b.cend(), 0);
  185. c.pop_back();
  186. assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
  187. a.assign(b.cbegin(), b.cend());
  188. assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
  189. a.assign(c.cbegin(), c.cend());
  190. assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
  191. a.assign(1, 2);
  192. assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
  193. a.reserve(100);
  194. assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
  195. }
  196. }
  197. int main(int argc, const char *argv[])
  198. {
  199. //#define SINGLE_TEST
  200. #define SIMPLE_IT
  201. #ifdef SINGLE_TEST
  202. #ifdef NDEBUG
  203. unsigned int numit [] = { 40 };
  204. #else
  205. unsigned int numit [] = { 4 };
  206. #endif
  207. unsigned int numele [] = { 10000 };
  208. #elif defined(SIMPLE_IT)
  209. unsigned int numit [] = { 3 };
  210. unsigned int numele [] = { 10000 };
  211. #else
  212. #ifdef NDEBUG
  213. unsigned int numit [] = { 40, 400, 4000, 40000 };
  214. #else
  215. unsigned int numit [] = { 4, 40, 400, 4000 };
  216. #endif
  217. unsigned int numele [] = { 10000, 1000, 100, 10 };
  218. #endif
  219. //Warning: range erasure is buggy. Vector iterators are not stable, so it is not
  220. //possible to cache iterators, but indexes!!!
  221. bool csv_output = argc == 2 && (strcmp(argv[1], "--csv-output") == 0);
  222. if(csv_output){
  223. print_header();
  224. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  225. stable_vector_test_template<get_stable_vector, StdAllocator>(numit[i], numele[i], csv_output);
  226. }
  227. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  228. stable_vector_test_template<get_vector, StdAllocator>(numit[i], numele[i], csv_output);
  229. }
  230. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  231. stable_vector_test_template<get_stable_vector, AllocatorPlusV1>(numit[i], numele[i], csv_output);
  232. }
  233. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  234. stable_vector_test_template<get_vector, AllocatorPlusV1>(numit[i], numele[i], csv_output);
  235. }
  236. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  237. stable_vector_test_template<get_stable_vector, AllocatorPlusV2>(numit[i], numele[i], csv_output);
  238. }
  239. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  240. stable_vector_test_template<get_vector, AllocatorPlusV2>(numit[i], numele[i], csv_output);
  241. }
  242. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  243. stable_vector_test_template<get_stable_vector, AdPool2PercentV2>(numit[i], numele[i], csv_output);
  244. }
  245. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  246. stable_vector_test_template<get_vector, AdPool2PercentV2>(numit[i], numele[i], csv_output);
  247. }
  248. }
  249. else{
  250. for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
  251. std::cout << "\n ----------------------------------- \n"
  252. << " Iterations/Elements: " << numit[i] << "/" << numele[i]
  253. << "\n ----------------------------------- \n";
  254. stable_vector_test_template<get_stable_vector, StdAllocator>(numit[i], numele[i], csv_output);
  255. stable_vector_test_template<get_vector, StdAllocator>(numit[i], numele[i], csv_output);
  256. stable_vector_test_template<get_stable_vector, AllocatorPlusV1>(numit[i], numele[i], csv_output);
  257. stable_vector_test_template<get_vector, AllocatorPlusV1>(numit[i], numele[i], csv_output);
  258. stable_vector_test_template<get_stable_vector, AllocatorPlusV2>(numit[i], numele[i], csv_output);
  259. stable_vector_test_template<get_vector, AllocatorPlusV2>(numit[i], numele[i], csv_output);
  260. stable_vector_test_template<get_stable_vector, AdPool2PercentV2>(numit[i], numele[i], csv_output);
  261. stable_vector_test_template<get_vector, AdPool2PercentV2>(numit[i], numele[i], csv_output);
  262. }
  263. }
  264. return 0;
  265. }