benchmark_strings.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 bsort = boost::sort;
  31. namespace bsc = boost::sort::common;
  32. using bsc::time_point;
  33. using bsc::now;
  34. using bsc::subtract_time;
  35. using bsc::fill_vector_uint64;
  36. using bsc::write_file_uint64;
  37. using bsort::spinsort;
  38. using bsort::flat_stable_sort;
  39. using bsort::spreadsort::spreadsort;
  40. using bsort::pdqsort;
  41. void Generator_random (void);
  42. void Generator_sorted(void);
  43. void Generator_sorted_end(size_t n_last);
  44. void Generator_sorted_middle (size_t n_last);
  45. void Generator_reverse_sorted(void);
  46. void Generator_reverse_sorted_end(size_t n_last);
  47. void Generator_reverse_sorted_middle(size_t n_last);
  48. template <class IA, class compare>
  49. int Test(std::vector<IA> &B, compare comp = compare());
  50. int main(int argc, char *argv[])
  51. {
  52. cout << "\n\n";
  53. cout << "************************************************************\n";
  54. cout << "** **\n";
  55. cout << "** B O O S T S O R T **\n";
  56. cout << "** S I N G L E T H R E A D **\n";
  57. cout << "** S T R I N G S B E N C H M A R K **\n";
  58. cout << "** **\n";
  59. cout << "** S O R T O F 10 000 000 S T R I N G S **\n";
  60. cout << "** **\n";
  61. cout << "************************************************************\n";
  62. cout << std::endl;
  63. cout<<"[ 1 ] std::sort [ 2 ] pdqsort [ 3 ] std::stable_sort \n";
  64. cout<<"[ 4 ] spinsort [ 5 ] flat_stable_sort [ 6 ] spreadsort\n\n";
  65. cout<<" | | | | | | |\n";
  66. cout<<" | [ 1 ]| [ 2 ]| [ 3 ]| [ 4 ]| [ 5 ]| [ 6 ]|\n";
  67. cout<<"--------------------+------+------+------+------+------+------+\n";
  68. std::string empty_line =
  69. " | | | | | | |\n";
  70. cout<<"random |";
  71. Generator_random ();
  72. cout<<empty_line;
  73. cout<<"sorted |";
  74. Generator_sorted();
  75. cout<<"sorted + 0.1% end |";
  76. Generator_sorted_end(NMAXSTRING / 1000);
  77. cout<<"sorted + 1% end |";
  78. Generator_sorted_end(NMAXSTRING / 100);
  79. cout<<"sorted + 10% end |";
  80. Generator_sorted_end(NMAXSTRING / 10);
  81. cout<<empty_line;
  82. cout<<"sorted + 0.1% mid |";
  83. Generator_sorted_middle (NMAXSTRING / 1000);
  84. cout<<"sorted + 1% mid |";
  85. Generator_sorted_middle (NMAXSTRING / 100);
  86. cout<<"sorted + 10% mid |";
  87. Generator_sorted_middle (NMAXSTRING / 10 );
  88. cout<<empty_line;
  89. cout<<"reverse sorted |";
  90. Generator_reverse_sorted();
  91. cout<<"rv sorted + 0.1% end|";
  92. Generator_reverse_sorted_end(NMAXSTRING / 1000);
  93. cout<<"rv sorted + 1% end|";
  94. Generator_reverse_sorted_end(NMAXSTRING / 100);
  95. cout<<"rv sorted + 10% end|";
  96. Generator_reverse_sorted_end(NMAXSTRING / 10);
  97. cout<<empty_line;
  98. cout<<"rv sorted + 0.1% mid|";
  99. Generator_reverse_sorted_middle(NMAXSTRING / 1000);
  100. cout<<"rv sorted + 1% mid|";
  101. Generator_reverse_sorted_middle(NMAXSTRING / 100);
  102. cout<<"rv sorted + 10% mid|";
  103. Generator_reverse_sorted_middle(NMAXSTRING / 10);
  104. cout<<"--------------------+------+------+------+------+------+------+\n";
  105. cout<<endl<<endl ;
  106. return 0;
  107. }
  108. void Generator_random(void)
  109. {
  110. std::vector<std::string> A;
  111. A.reserve(NMAXSTRING);
  112. A.clear();
  113. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING) != 0)
  114. {
  115. std::cout << "Error in the input file\n";
  116. return;
  117. };
  118. Test<std::string, std::less<std::string>>(A);
  119. };
  120. void Generator_sorted(void)
  121. {
  122. std::vector<std::string> A;
  123. A.reserve(NMAXSTRING);
  124. A.clear();
  125. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING) != 0)
  126. {
  127. std::cout << "Error in the input file\n";
  128. return;
  129. };
  130. std::sort( A.begin() , A.end() );
  131. Test<std::string, std::less<std::string>>(A);
  132. };
  133. void Generator_sorted_end(size_t n_last)
  134. {
  135. std::vector<std::string> A;
  136. A.reserve(NMAXSTRING);
  137. A.clear();
  138. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING+ n_last) != 0)
  139. {
  140. std::cout << "Error in the input file\n";
  141. return;
  142. };
  143. std::sort (A.begin() , A.begin() + NMAXSTRING );
  144. Test<std::string, std::less<std::string>>(A);
  145. };
  146. void Generator_sorted_middle(size_t n_last)
  147. {
  148. std::vector<std::string> A,B,C;
  149. A.reserve(NMAXSTRING);
  150. A.clear();
  151. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING + n_last) != 0)
  152. {
  153. std::cout << "Error in the input file\n";
  154. return;
  155. };
  156. for ( size_t i = NMAXSTRING ; i < A.size() ; ++i)
  157. B.push_back ( std::move ( A[i]));
  158. A.resize ( NMAXSTRING);
  159. std::sort (A.begin() , A.end() );
  160. size_t step = NMAXSTRING /n_last +1 ;
  161. size_t pos = 0 ;
  162. for ( size_t i =0 ; i < B.size() ; ++i, pos += step)
  163. { C.push_back ( B[i]);
  164. for ( size_t k = 0 ; k < step ; ++k )
  165. C.push_back ( A[pos + k] );
  166. };
  167. while ( pos < A.size() ) C.push_back ( A[pos++]);
  168. A = C ;
  169. Test<std::string, std::less<std::string>>(A);
  170. };
  171. void Generator_reverse_sorted(void)
  172. {
  173. std::vector<std::string> A;
  174. A.reserve(NMAXSTRING);
  175. {
  176. std::vector<std::string> B;
  177. B.reserve(NMAXSTRING);
  178. if (bsc::fill_vector_string("input.bin", B, NMAXSTRING) != 0)
  179. {
  180. std::cout << "Error in the input file\n";
  181. return;
  182. };
  183. std::sort(B.begin(), B.end());
  184. A.clear();
  185. for (size_t i = 0; i < NMAXSTRING; ++i)
  186. A.push_back(B[NMAXSTRING - 1 - i]);
  187. };
  188. Test<std::string, std::less<std::string>>(A);
  189. /*
  190. std::vector<std::string> A;
  191. A.reserve(NMAXSTRING);
  192. A.clear();
  193. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING) != 0)
  194. {
  195. std::cout << "Error in the input file\n";
  196. return;
  197. };
  198. std::sort( A.begin() , A.end());
  199. size_t nmid = (A.size() >>1), nlast = A.size() -1 ;
  200. for (size_t i = 0 ; i < nmid ;++i)
  201. std::swap ( A[i], A [nlast -i]);
  202. Test<std::string, std::less<std::string>>(A);
  203. */
  204. };
  205. void Generator_reverse_sorted_end(size_t n_last)
  206. {
  207. std::vector<std::string> A;
  208. A.reserve(NMAXSTRING);
  209. A.clear();
  210. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING+ n_last) != 0)
  211. {
  212. std::cout << "Error in the input file\n";
  213. return;
  214. };
  215. std::sort (A.begin() , A.begin() + NMAXSTRING );
  216. for ( size_t i =0 ; i< (NMAXSTRING >>1); ++i)
  217. std::swap ( A[i], A[NMAXSTRING - 1 - i]);
  218. Test<std::string, std::less<std::string>>(A);
  219. };
  220. void Generator_reverse_sorted_middle(size_t n_last)
  221. {
  222. std::vector<std::string> A,B,C;
  223. A.reserve(NMAXSTRING);
  224. A.clear();
  225. if (bsc::fill_vector_string("input.bin", A, NMAXSTRING + n_last) != 0)
  226. {
  227. std::cout << "Error in the input file\n";
  228. return;
  229. };
  230. for ( size_t i = NMAXSTRING ; i < A.size() ; ++i)
  231. B.push_back ( std::move ( A[i]));
  232. A.resize ( NMAXSTRING);
  233. std::sort (A.begin() , A.end() );
  234. for ( size_t i =0 ; i< (NMAXSTRING >>1); ++i)
  235. std::swap ( A[i], A[NMAXSTRING - 1 - i]);
  236. size_t step = NMAXSTRING /n_last +1 ;
  237. size_t pos = 0 ;
  238. for ( size_t i =0 ; i < B.size() ; ++i, pos += step)
  239. { C.push_back ( B[i]);
  240. for ( size_t k = 0 ; k < step ; ++k )
  241. C.push_back ( A[pos + k] );
  242. };
  243. while ( pos < A.size() )
  244. C.push_back ( A[pos++]);
  245. A = C ;
  246. Test<std::string, std::less<std::string>>(A);
  247. };
  248. template<class IA, class compare>
  249. int Test (std::vector<IA> &B, compare comp)
  250. { //---------------------------- begin -----------------------------
  251. double duration;
  252. time_point start, finish;
  253. std::vector<IA> A (B);
  254. std::vector<double> V;
  255. A = B;
  256. start = now ();
  257. std::sort (A.begin (), A.end (), comp);
  258. finish = now ();
  259. duration = subtract_time (finish, start);
  260. V.push_back (duration);
  261. A = B;
  262. start = now ();
  263. pdqsort (A.begin (), A.end (), comp);
  264. finish = now ();
  265. duration = subtract_time (finish, start);
  266. V.push_back (duration);
  267. A = B;
  268. start = now ();
  269. std::stable_sort (A.begin (), A.end (), comp);
  270. finish = now ();
  271. duration = subtract_time (finish, start);
  272. V.push_back (duration);
  273. A = B;
  274. start = now ();
  275. spinsort (A.begin (), A.end (), comp);
  276. finish = now ();
  277. duration = subtract_time (finish, start);
  278. V.push_back (duration);
  279. A = B;
  280. start = now ();
  281. flat_stable_sort (A.begin (), A.end (), comp);
  282. finish = now ();
  283. duration = subtract_time (finish, start);
  284. V.push_back (duration);
  285. A = B;
  286. start = now ();
  287. spreadsort (A.begin (), A.end ());
  288. finish = now ();
  289. duration = subtract_time (finish, start);
  290. V.push_back (duration);
  291. //-----------------------------------------------------------------------
  292. // printing the vector
  293. //-----------------------------------------------------------------------
  294. std::cout<<std::setprecision(2)<<std::fixed;
  295. for ( uint32_t i =0 ; i < V.size() ; ++i)
  296. { std::cout<<std::right<<std::setw(5)<<V[i]<<" |";
  297. };
  298. std::cout<<std::endl;
  299. return 0;
  300. };