binaryalrbreaker.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // a sorting example that uses the worst-case distribution for spreadsort.
  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. const DATA_TYPE typed_one = 1;
  28. void
  29. fill_vector(vector<DATA_TYPE> & input, const DATA_TYPE base_value,
  30. unsigned remaining_bits, const vector<unsigned> & indices,
  31. int index)
  32. {
  33. if (index < 0) {
  34. for (unsigned u = 0; u < max_count; ++u)
  35. input.push_back((base_value << remaining_bits) +
  36. (rand() % (1 << remaining_bits)));
  37. }
  38. else {
  39. unsigned shift = indices[index];
  40. fill_vector(input, (base_value << shift) + ((1 << shift) - 1),
  41. remaining_bits - shift, indices, index - 1);
  42. fill_vector(input, base_value << shift, remaining_bits - shift, indices,
  43. index - 1);
  44. }
  45. }
  46. //Generates a random index from 0 up to but not including count.
  47. unsigned
  48. get_index(unsigned count)
  49. {
  50. unsigned result = unsigned((rand() % (1 << 16))*uint64_t(count)/(1 << 16));
  51. if (result >= count)
  52. return count - 1;
  53. return result;
  54. }
  55. //Tests std::sort vs boost::sort::spreadsort on boost::sort's worst distribution.
  56. int main(int, const char **) {
  57. unsigned total_length = sizeof(DATA_TYPE) * 8;
  58. double std_sort_time = 0;
  59. double spreadsort_time = 0;
  60. for (int repetition = 0; repetition < 10; ++repetition) {
  61. vector<DATA_TYPE> input;
  62. vector<unsigned> offsets;
  63. unsigned bit_length = total_length - radix_threshold;
  64. unsigned bit_offset = bit_shift;
  65. for (; bit_length >= ++bit_offset; bit_length -= bit_offset)
  66. offsets.push_back(bit_offset);
  67. for (int ii = (1 << bit_length) - 1; ii >= 0; --ii)
  68. fill_vector(input, ii, total_length - bit_length,
  69. offsets, offsets.size() - 1);
  70. //Randomize the inputs slightly so they aren't in reverse-sorted order, for
  71. //which std::sort is very fast.
  72. for (unsigned u = 0; u < input.size() / 10; ++u)
  73. std::swap(input[get_index(input.size())], input[get_index(input.size())]);
  74. //Run both std::sort and boost::sort::spreadsort.
  75. for (unsigned u = 0; u < 2; ++u) {
  76. vector<DATA_TYPE> array = input;
  77. clock_t start, end;
  78. double elapsed;
  79. start = clock();
  80. if (u)
  81. std::sort(array.begin(), array.end());
  82. else
  83. boost::sort::spreadsort::spreadsort(array.begin(), array.end());
  84. end = clock();
  85. elapsed = static_cast<double>(end - start);
  86. if (u)
  87. std_sort_time += elapsed / CLOCKS_PER_SEC;
  88. else
  89. spreadsort_time += elapsed / CLOCKS_PER_SEC;
  90. array.clear();
  91. }
  92. }
  93. printf("std::sort elapsed time %f\n", std_sort_time);
  94. printf("spreadsort elapsed time %f\n", spreadsort_time);
  95. return 0;
  96. }