int64.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // spreadsort 64-bit integer sorting 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. //[int64bit_1
  21. #define DATA_TYPE boost::int64_t
  22. //] [/int64bit_1]
  23. //Pass in an argument to test std::sort
  24. int main(int argc, const char ** argv) {
  25. size_t uCount,uSize=sizeof(DATA_TYPE);
  26. bool stdSort = false;
  27. unsigned loopCount = 1;
  28. for (int u = 1; u < argc; ++u) {
  29. if (std::string(argv[u]) == "-std")
  30. stdSort = true;
  31. else
  32. loopCount = atoi(argv[u]);
  33. }
  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. //Run multiple loops, if requested
  45. for (unsigned u = 0; u < loopCount; ++u) {
  46. input.seekg (0, std::ios_base::beg);
  47. //Conversion to a vector
  48. array.resize(uCount);
  49. unsigned v = 0;
  50. while (input.good() && v < uCount)
  51. input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
  52. if (v < uCount)
  53. array.resize(v);
  54. clock_t start, end;
  55. double elapsed;
  56. start = clock();
  57. if (stdSort)
  58. //std::sort(&(array[0]), &(array[0]) + uCount);
  59. std::sort(array.begin(), array.end());
  60. else
  61. //boost::sort::spreadsort::spreadsort(&(array[0]), &(array[0]) + uCount);
  62. //[int64bit_2
  63. boost::sort::spreadsort::spreadsort(array.begin(), array.end());
  64. //] [/int64bit_2]
  65. end = clock();
  66. elapsed = static_cast<double>(end - start) ;
  67. std::ofstream ofile;
  68. if (stdSort)
  69. ofile.open("standard_sort_out.txt", std::ios_base::out |
  70. std::ios_base::binary | std::ios_base::trunc);
  71. else
  72. ofile.open("boost_sort_out.txt", std::ios_base::out |
  73. std::ios_base::binary | std::ios_base::trunc);
  74. if (ofile.good()) {
  75. for (unsigned v = 0; v < array.size(); ++v) {
  76. ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
  77. }
  78. ofile.close();
  79. }
  80. total += elapsed;
  81. array.clear();
  82. }
  83. input.close();
  84. if (stdSort)
  85. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  86. else
  87. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  88. return 0;
  89. }