benchmark_strings.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //----------------------------------------------------------------------------
  2. /// @file benchmark_strings.cpp
  3. /// @brief Benchmark of several sort methods with strings
  4. ///
  5. /// @author Copyright (c) 2017 Francisco José Tapia (fjtapia@gmail.com )\n
  6. /// Distributed under the Boost Software License, Version 1.0.\n
  7. /// ( See accompanying file LICENSE_1_0.txt or copy at
  8. /// http://www.boost.org/LICENSE_1_0.txt )
  9. ///
  10. /// This program use for comparison purposes, the Threading Building
  11. /// Blocks which license is the GNU General Public License, version 2
  12. /// as published by the Free Software Foundation.
  13. ///
  14. /// @version 0.1
  15. ///
  16. /// @remarks
  17. //-----------------------------------------------------------------------------
  18. #include <algorithm>
  19. #include <iostream>
  20. #include <iomanip>
  21. #include <random>
  22. #include <stdlib.h>
  23. #include <vector>
  24. #include <boost/sort/common/time_measure.hpp>
  25. #include <boost/sort/common/file_vector.hpp>
  26. #include <boost/sort/common/int_array.hpp>
  27. #include <boost/sort/sort.hpp>
  28. #define NMAXSTRING 10000000
  29. using namespace std;
  30. namespace bsc = boost::sort::common;
  31. namespace bsp = boost::sort;
  32. using bsc::time_point;
  33. using bsc::now;
  34. using bsc::subtract_time;
  35. void Generator_random (void);
  36. void Generator_sorted(void);
  37. void Generator_sorted_end(size_t n_last);
  38. void Generator_sorted_middle (size_t n_last);
  39. void Generator_reverse_sorted(void);
  40. void Generator_reverse_sorted_end(size_t n_last);
  41. void Generator_reverse_sorted_middle(size_t n_last);
  42. template <class IA, class compare>
  43. int Test(std::vector<IA> &B, compare comp = compare());
  44. int main(int argc, char *argv[])
  45. {
  46. cout << "\n\n";
  47. cout << "************************************************************\n";
  48. cout << "** **\n";
  49. cout << "** B O O S T S O R T P A R A L L E L **\n";
  50. cout << "** S T R I N G S B E N C H M A R K **\n";
  51. cout << "** **\n";
  52. cout << "** S O R T O F 10 000 000 S T R I N G S **\n";
  53. cout << "** **\n";
  54. cout << "************************************************************\n";
  55. cout << std::endl;
  56. cout<<"[ 1 ] block_indirect_sort [ 2 ] sample_sort\n";
  57. cout<<"[ 3 ] parallel_stable_sort\n\n";
  58. cout<<" | | | |\n";
  59. cout<<" | [ 1 ]| [ 2 ]| [ 3 ]|\n";
  60. cout<<"--------------------+------+------+------+\n";
  61. std::string empty_line =
  62. " | | | |\n";
  63. cout<<"random |";
  64. Generator_random ();
  65. cout<<empty_line;
  66. cout<<"sorted |";
  67. Generator_sorted();
  68. cout<<"sorted + 0.1% end |";
  69. Generator_sorted_end(NMAXSTRING / 1000);
  70. cout<<"sorted + 1% end |";
  71. Generator_sorted_end(NMAXSTRING / 100);
  72. cout<<"sorted + 10% end |";
  73. Generator_sorted_end(NMAXSTRING / 10);
  74. cout<<empty_line;
  75. cout<<"sorted + 0.1% mid |";
  76. Generator_sorted_middle (NMAXSTRING / 1000);
  77. cout<<"sorted + 1% mid |";
  78. Generator_sorted_middle (NMAXSTRING / 100);
  79. cout<<"sorted + 10% mid |";
  80. Generator_sorted_middle (NMAXSTRING / 10 );
  81. cout<<empty_line;
  82. cout<<"reverse sorted |";
  83. Generator_reverse_sorted();
  84. cout<<"rv sorted + 0.1% end|";
  85. Generator_reverse_sorted_end(NMAXSTRING / 1000);
  86. cout<<"rv sorted + 1% end|";
  87. Generator_reverse_sorted_end(NMAXSTRING / 100);
  88. cout<<"rv sorted + 10% end|";
  89. Generator_reverse_sorted_end(NMAXSTRING / 10);
  90. cout<<empty_line;
  91. cout<<"rv sorted + 0.1% mid|";
  92. Generator_reverse_sorted_middle(NMAXSTRING / 1000);
  93. cout<<"rv sorted + 1% mid|";
  94. Generator_reverse_sorted_middle(NMAXSTRING / 100);
  95. cout<<"rv sorted + 10% mid|";
  96. Generator_reverse_sorted_middle(NMAXSTRING / 10);
  97. cout<< "--------------------+------+------+------+\n";
  98. cout<<endl<<endl ;
  99. return 0;
  100. }
  101. void Generator_random(void)
  102. {
  103. std::vector<std::string> A;
  104. A.reserve(NMAXSTRING);
  105. A.clear();
  106. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING) != 0)
  107. {
  108. std::cout << "Error in the input file\n";
  109. return;
  110. };
  111. Test<std::string, std::less<std::string>>(A);
  112. };
  113. void Generator_sorted(void)
  114. {
  115. std::vector<std::string> A;
  116. A.reserve(NMAXSTRING);
  117. A.clear();
  118. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING) != 0)
  119. {
  120. std::cout << "Error in the input file\n";
  121. return;
  122. };
  123. std::sort( A.begin() , A.end() );
  124. Test<std::string, std::less<std::string>>(A);
  125. };
  126. void Generator_sorted_end(size_t n_last)
  127. {
  128. std::vector<std::string> A;
  129. A.reserve(NMAXSTRING);
  130. A.clear();
  131. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING+ n_last) != 0)
  132. {
  133. std::cout << "Error in the input file\n";
  134. return;
  135. };
  136. std::sort (A.begin() , A.begin() + NMAXSTRING );
  137. Test<std::string, std::less<std::string>>(A);
  138. };
  139. void Generator_sorted_middle(size_t n_last)
  140. {
  141. std::vector<std::string> A,B,C;
  142. A.reserve(NMAXSTRING);
  143. A.clear();
  144. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING + n_last) != 0)
  145. {
  146. std::cout << "Error in the input file\n";
  147. return;
  148. };
  149. for ( size_t i = NMAXSTRING ; i < A.size() ; ++i)
  150. B.push_back ( std::move ( A[i]));
  151. A.resize ( NMAXSTRING);
  152. std::sort (A.begin() , A.end() );
  153. size_t step = NMAXSTRING /n_last +1 ;
  154. size_t pos = 0 ;
  155. for ( size_t i =0 ; i < B.size() ; ++i, pos += step)
  156. { C.push_back ( B[i]);
  157. for ( size_t k = 0 ; k < step ; ++k )
  158. C.push_back ( A[pos + k] );
  159. };
  160. while ( pos < A.size() ) C.push_back ( A[pos++]);
  161. A = C ;
  162. Test<std::string, std::less<std::string>>(A);
  163. };
  164. void Generator_reverse_sorted(void)
  165. {
  166. std::vector<std::string> A;
  167. A.reserve(NMAXSTRING);
  168. A.clear();
  169. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING) != 0)
  170. {
  171. std::cout << "Error in the input file\n";
  172. return;
  173. };
  174. std::sort( A.begin() , A.end());
  175. size_t nmid = (A.size() >>1), nlast = A.size() -1 ;
  176. for (size_t i = 0 ; i < nmid ;++i)
  177. std::swap ( A[i], A [nlast -i]);
  178. Test<std::string, std::less<std::string>>(A);
  179. };
  180. void Generator_reverse_sorted_end(size_t n_last)
  181. {
  182. std::vector<std::string> A;
  183. A.reserve(NMAXSTRING);
  184. A.clear();
  185. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING+ n_last) != 0)
  186. {
  187. std::cout << "Error in the input file\n";
  188. return;
  189. };
  190. std::sort (A.begin() , A.begin() + NMAXSTRING );
  191. for ( size_t i =0 ; i< (NMAXSTRING >>1); ++i)
  192. std::swap ( A[i], A[NMAXSTRING - 1 - i]);
  193. Test<std::string, std::less<std::string>>(A);
  194. };
  195. void Generator_reverse_sorted_middle(size_t n_last)
  196. {
  197. std::vector<std::string> A,B,C;
  198. A.reserve(NMAXSTRING);
  199. A.clear();
  200. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING + n_last) != 0)
  201. {
  202. std::cout << "Error in the input file\n";
  203. return;
  204. };
  205. for ( size_t i = NMAXSTRING ; i < A.size() ; ++i)
  206. B.push_back ( std::move ( A[i]));
  207. A.resize ( NMAXSTRING);
  208. std::sort (A.begin() , A.end() );
  209. for ( size_t i =0 ; i< (NMAXSTRING >>1); ++i)
  210. std::swap ( A[i], A[NMAXSTRING - 1 - i]);
  211. size_t step = NMAXSTRING /n_last +1 ;
  212. size_t pos = 0 ;
  213. for ( size_t i =0 ; i < B.size() ; ++i, pos += step)
  214. { C.push_back ( B[i]);
  215. for ( size_t k = 0 ; k < step ; ++k )
  216. C.push_back ( A[pos + k] );
  217. };
  218. while ( pos < A.size() )
  219. C.push_back ( A[pos++]);
  220. A = C ;
  221. Test<std::string, std::less<std::string>>(A);
  222. };
  223. template<class IA, class compare>
  224. int Test (std::vector<IA> &B, compare comp)
  225. { //---------------------------- begin --------------------------------
  226. double duration;
  227. time_point start, finish;
  228. std::vector<IA> A (B);
  229. std::vector<double> V;
  230. //--------------------------------------------------------------------
  231. A = B;
  232. start = now ();
  233. bsp::block_indirect_sort (A.begin (), A.end (), comp);
  234. finish = now ();
  235. duration = subtract_time (finish, start);
  236. V.push_back (duration);
  237. A = B;
  238. start = now ();
  239. bsp::sample_sort (A.begin (), A.end (), comp);
  240. finish = now ();
  241. duration = subtract_time (finish, start);
  242. V.push_back (duration);
  243. A = B;
  244. start = now ();
  245. bsp::parallel_stable_sort (A.begin (), A.end (), comp);
  246. finish = now ();
  247. duration = subtract_time (finish, start);
  248. V.push_back (duration);
  249. //-----------------------------------------------------------------------
  250. // printing the vector
  251. //-----------------------------------------------------------------------
  252. std::cout<<std::setprecision(2)<<std::fixed;
  253. for ( uint32_t i =0 ; i < V.size() ; ++i)
  254. { std::cout<<std::right<<std::setw(5)<<V[i]<<" |";
  255. };
  256. std::cout<<std::endl;
  257. return 0;
  258. };