heap_benchmarks.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*=============================================================================
  2. Copyright (c) 2010 Tim Blechmann
  3. Use, modification and distribution is subject to the Boost Software
  4. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #include <algorithm>
  8. #include <vector>
  9. #include <boost/foreach.hpp>
  10. #include "high_resolution_timer.hpp"
  11. #include <boost/heap/heap_merge.hpp>
  12. #if defined(__GNUC__) && (!defined __INTEL_COMPILER)
  13. #define no_inline __attribute__ ((noinline))
  14. #else
  15. #define no_inline
  16. #endif
  17. typedef std::vector<int> test_data;
  18. const int num_benchmarks = 1;
  19. inline test_data make_sequential_test_data(int size)
  20. {
  21. test_data v(size);
  22. for (int i = 0; i != size; ++i)
  23. v[i] = i;
  24. return v;
  25. }
  26. inline test_data make_test_data(int size)
  27. {
  28. test_data v = make_sequential_test_data(size);
  29. std::random_shuffle(v.begin(), v.end());
  30. return v;
  31. }
  32. const int max_data = 20;
  33. test_data const & get_data(int index)
  34. {
  35. static std::vector <test_data> data;
  36. if (data.empty())
  37. for (int i = 0; i != num_benchmarks; ++i)
  38. data.push_back(make_test_data((1<<(max_data+1))+1024));
  39. return data[index];
  40. }
  41. #define DEFINE_BENCHMARKS_SELECTOR(SUFFIX) \
  42. struct make_##SUFFIX \
  43. { \
  44. template <typename heap> \
  45. struct rebind { \
  46. typedef run_##SUFFIX<heap> type; \
  47. }; \
  48. };
  49. template <typename pri_queue>
  50. void fill_heap(pri_queue & q, int index, int size, int offset = 0)
  51. {
  52. test_data const & data = get_data(index);
  53. for (int i = 0; i != size + 1; ++i) {
  54. q.push(data[i]);
  55. int top = q.top();
  56. q.pop();
  57. q.push(top);
  58. }
  59. }
  60. template <typename pri_queue,
  61. typename handle_container
  62. >
  63. void fill_heap_with_handles(pri_queue & q, handle_container & handles, int index, int size, int offset = 0)
  64. {
  65. test_data const & data = get_data(index);
  66. for (int i = 0; i != size + 1; ++i) {
  67. handles[i] = q.push(data[i]);
  68. if (i > 0) {
  69. typename pri_queue::handle_type last = handles[i-1];
  70. int val = *last;
  71. q.erase(last);
  72. handles[i-1] = q.push(val);
  73. }
  74. }
  75. }
  76. template <typename pri_queue>
  77. struct run_sequential_push
  78. {
  79. run_sequential_push(int size):
  80. size(size)
  81. {}
  82. void prepare(int index)
  83. {
  84. q.clear();
  85. fill_heap(q, index, size);
  86. }
  87. no_inline void operator()(int index)
  88. {
  89. test_data const & data = get_data(index);
  90. for (int i = 0; i != 16; ++i)
  91. q.push(data[(1<<max_data) + i]);
  92. }
  93. pri_queue q;
  94. int size;
  95. };
  96. DEFINE_BENCHMARKS_SELECTOR(sequential_push)
  97. template <typename pri_queue>
  98. struct run_sequential_pop
  99. {
  100. run_sequential_pop(int size):
  101. size(size)
  102. {}
  103. void prepare(int index)
  104. {
  105. q.clear();
  106. fill_heap(q, index, size);
  107. }
  108. no_inline void operator()(int index)
  109. {
  110. for (int i = 0; i != 16; ++i)
  111. q.pop();
  112. }
  113. pri_queue q;
  114. int size;
  115. };
  116. DEFINE_BENCHMARKS_SELECTOR(sequential_pop)
  117. template <typename pri_queue>
  118. struct run_sequential_increase
  119. {
  120. run_sequential_increase(int size):
  121. size(size), handles(size)
  122. {}
  123. void prepare(int index)
  124. {
  125. q.clear();
  126. fill_heap_with_handles(q, handles, index, size);
  127. }
  128. no_inline void operator()(int index)
  129. {
  130. test_data const & data = get_data(index);
  131. for (int i = 0; i != 16; ++i)
  132. q.increase(handles[i], data[i] + (1<<max_data));
  133. }
  134. pri_queue q;
  135. int size;
  136. std::vector<typename pri_queue::handle_type> handles;
  137. };
  138. DEFINE_BENCHMARKS_SELECTOR(sequential_increase)
  139. template <typename pri_queue>
  140. struct run_sequential_decrease
  141. {
  142. run_sequential_decrease(int size):
  143. size(size), handles(size)
  144. {}
  145. void prepare(int index)
  146. {
  147. q.clear();
  148. fill_heap_with_handles(q, handles, index, size);
  149. }
  150. no_inline void operator()(int index)
  151. {
  152. test_data const & data = get_data(index);
  153. for (int i = 0; i != 16; ++i)
  154. q.decrease(handles[i], data[i] + (1<<max_data));
  155. }
  156. pri_queue q;
  157. int size;
  158. std::vector<typename pri_queue::handle_type> handles;
  159. };
  160. DEFINE_BENCHMARKS_SELECTOR(sequential_decrease)
  161. template <typename pri_queue>
  162. struct run_merge_and_clear
  163. {
  164. run_merge_and_clear(int size):
  165. size(size)
  166. {}
  167. void prepare(int index)
  168. {
  169. q.clear();
  170. r.clear();
  171. fill_heap(q, index, size);
  172. fill_heap(r, index, size, size);
  173. }
  174. no_inline void operator()(int index)
  175. {
  176. boost::heap::heap_merge(q, r);
  177. }
  178. pri_queue q, r;
  179. int size;
  180. };
  181. DEFINE_BENCHMARKS_SELECTOR(merge_and_clear)
  182. template <typename pri_queue>
  183. struct run_equivalence
  184. {
  185. run_equivalence(int size):
  186. size(size)
  187. {}
  188. void prepare(int index)
  189. {
  190. q.clear();
  191. r.clear();
  192. s.clear();
  193. fill_heap(q, index, size);
  194. fill_heap(r, index, size, size);
  195. fill_heap(s, index, size);
  196. }
  197. no_inline bool operator()(int index)
  198. {
  199. return (q == r) ^ (q == s);
  200. }
  201. pri_queue q, r, s;
  202. int size;
  203. };
  204. DEFINE_BENCHMARKS_SELECTOR(equivalence)
  205. template <typename benchmark>
  206. inline double run_benchmark(benchmark & b)
  207. {
  208. boost::high_resolution_timer timer;
  209. std::vector<double> results;
  210. for (int i = 0; i != num_benchmarks; ++i)
  211. {
  212. b.prepare(i);
  213. timer.restart();
  214. b(i);
  215. double t = timer.elapsed();
  216. results.push_back(t);
  217. }
  218. return results.at(results.size()/2); // median
  219. }