alreadysorted.cpp 2.7 KB

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