alrbreaker.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // a sorting example that uses the worst-case for conventional MSD radix sorts.
  2. //
  3. // Copyright Steven Ross 2009-2014.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // See http://www.boost.org/libs/sort for library home page.
  9. #include <boost/sort/spreadsort/spreadsort.hpp>
  10. #include <time.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <string>
  16. #include <fstream>
  17. #include <sstream>
  18. #include <iostream>
  19. using namespace boost::sort::spreadsort;
  20. using namespace std;
  21. #define DATA_TYPE boost::uint64_t
  22. #define ALR_THRESHOLD 3
  23. const unsigned max_count = ALR_THRESHOLD - 1;
  24. const unsigned bit_shift = detail::rough_log_2_size(max_count) -
  25. detail::int_log_mean_bin_size;
  26. const unsigned radix_threshold = detail::rough_log_2_size(max_count) + 1;
  27. //Increase this size if too fast to test accurately
  28. const unsigned top_splits = 12;
  29. const DATA_TYPE typed_one = 1;
  30. void
  31. fill_vector(vector<DATA_TYPE> & input, const DATA_TYPE base_value,
  32. unsigned remaining_bits)
  33. {
  34. if (remaining_bits >= radix_threshold) {
  35. input.push_back((base_value << remaining_bits) +
  36. ((typed_one << remaining_bits) - 1));
  37. fill_vector(input, base_value << bit_shift, remaining_bits - bit_shift);
  38. }
  39. else {
  40. for (unsigned u = 0; u < max_count; ++u)
  41. input.push_back((base_value << remaining_bits) +
  42. (rand() % (1 << remaining_bits)));
  43. }
  44. }
  45. //Tests spreadsort on the worst-case distribution for standard MSD radix sorts.
  46. int main(int, const char **) {
  47. vector<DATA_TYPE> input;
  48. for (int ii = (1 << top_splits) - 1; ii >= 0; --ii)
  49. fill_vector(input, ii, (sizeof(DATA_TYPE) * 8) - top_splits);
  50. //Run both std::sort and spreadsort
  51. for (unsigned u = 0; u < 2; ++u) {
  52. vector<DATA_TYPE> array = input;
  53. clock_t start, end;
  54. double elapsed;
  55. start = clock();
  56. if (u)
  57. std::sort(array.begin(), array.end());
  58. else
  59. boost::sort::spreadsort::spreadsort(array.begin(), array.end());
  60. end = clock();
  61. elapsed = static_cast<double>(end - start);
  62. if (u)
  63. printf("std::sort elapsed time %f\n", elapsed / CLOCKS_PER_SEC);
  64. else
  65. printf("spreadsort elapsed time %f\n", elapsed / CLOCKS_PER_SEC);
  66. array.clear();
  67. }
  68. return 0;
  69. }