mostlysorted.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // spreadsort on a mostly sorted array example.
  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. #define DATA_TYPE int
  21. unsigned
  22. get_index(unsigned count)
  23. {
  24. unsigned result = unsigned((rand() % (1 << 16))*uint64_t(count)/(1 << 16));
  25. if (result >= count || result < 0)
  26. result = count - 1;
  27. return result;
  28. }
  29. //Pass in an argument to test std::sort
  30. int main(int argc, const char ** argv) {
  31. srand(1);
  32. size_t uCount,uSize=sizeof(DATA_TYPE);
  33. bool stdSort = false;
  34. unsigned loopCount = 1;
  35. for (int u = 1; u < argc; ++u) {
  36. if (std::string(argv[u]) == "-std")
  37. stdSort = true;
  38. else
  39. loopCount = atoi(argv[u]);
  40. }
  41. //Sorts the data once, then times sorting of already-sorted data
  42. loopCount += 1;
  43. std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
  44. if (input.fail()) {
  45. printf("input.txt could not be opened\n");
  46. return 1;
  47. }
  48. double total = 0.0;
  49. std::vector<DATA_TYPE> array;
  50. input.seekg (0, std::ios_base::end);
  51. size_t length = input.tellg();
  52. uCount = length/uSize;
  53. input.seekg (0, std::ios_base::beg);
  54. //Conversion to a vector
  55. array.resize(uCount);
  56. unsigned v = 0;
  57. while (input.good() && v < uCount) // EOF or failure stops the reading
  58. input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
  59. //Run multiple loops, if requested
  60. for (unsigned u = 0; u < loopCount; ++u) {
  61. for (unsigned v = 0; v < uCount/10; ++v)
  62. std::swap(array[get_index(uCount)], array[get_index(uCount)]);
  63. clock_t start, end;
  64. double elapsed;
  65. start = clock();
  66. if (stdSort)
  67. //std::sort(&(array[0]), &(array[0]) + uCount);
  68. std::sort(array.begin(), array.end());
  69. else
  70. //integer_sort(&(array[0]), &(array[0]) + uCount);
  71. integer_sort(array.begin(), array.end());
  72. end = clock();
  73. elapsed = static_cast<double>(end - start) ;
  74. std::ofstream ofile;
  75. if (stdSort)
  76. ofile.open("standard_sort_out.txt", std::ios_base::out |
  77. std::ios_base::binary | std::ios_base::trunc);
  78. else
  79. ofile.open("boost_sort_out.txt", std::ios_base::out |
  80. std::ios_base::binary | std::ios_base::trunc);
  81. if (ofile.good()) {
  82. for (unsigned v = 0; v < array.size(); ++v) {
  83. ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
  84. }
  85. ofile.close();
  86. }
  87. if (u)
  88. total += elapsed;
  89. }
  90. if (stdSort)
  91. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  92. else
  93. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  94. return 0;
  95. }