rightshiftsample.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //! \brief Integer sort with a rightshift functor sorting example.
  2. //! \file
  3. //
  4. // Copyright Steven Ross 2009-2014.
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // See http://www.boost.org/libs/sort for library home page.
  10. // Caution: this file contains Quickbook markup as well as code
  11. // and comments, don't change any of the special comment markups!
  12. #include <boost/sort/spreadsort/spreadsort.hpp>
  13. #include <time.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <algorithm>
  17. #include <vector>
  18. #include <string>
  19. #include <fstream>
  20. #include <iostream>
  21. using namespace boost::sort::spreadsort;
  22. #define DATA_TYPE int
  23. //[rightshift_int_functor
  24. struct rightshift {
  25. inline int operator()(DATA_TYPE x, unsigned offset) { return x >> offset; }
  26. };
  27. //] [/rightshift_int_functor]
  28. //Pass in an argument to test std::sort
  29. int main(int argc, const char ** argv) {
  30. size_t uCount,uSize=sizeof(DATA_TYPE);
  31. bool stdSort = false;
  32. unsigned loopCount = 1;
  33. for (int u = 1; u < argc; ++u) {
  34. if (std::string(argv[u]) == "-std")
  35. stdSort = true;
  36. else
  37. loopCount = atoi(argv[u]);
  38. }
  39. std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
  40. if (input.fail()) {
  41. printf("input.txt could not be opened\n");
  42. return 1;
  43. }
  44. double total = 0.0;
  45. std::vector<DATA_TYPE> array;
  46. input.seekg (0, std::ios_base::end);
  47. size_t length = input.tellg();
  48. uCount = length/uSize;
  49. //Run multiple loops, if requested
  50. for (unsigned u = 0; u < loopCount; ++u) {
  51. input.seekg (0, std::ios_base::beg);
  52. //Conversion to a vector
  53. array.resize(uCount);
  54. unsigned v = 0;
  55. while (input.good() && v < uCount)
  56. input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
  57. if (v < uCount)
  58. array.resize(v);
  59. clock_t start, end;
  60. double elapsed;
  61. start = clock();
  62. if (stdSort) {
  63. std::sort(array.begin(), array.end());
  64. } else {
  65. //[rightshift_1
  66. integer_sort(array.begin(), array.end(), rightshift());
  67. //] [/rightshift_1]
  68. }
  69. end = clock();
  70. elapsed = static_cast<double>(end - start) ;
  71. std::ofstream ofile;
  72. if (stdSort)
  73. ofile.open("standard_sort_out.txt", std::ios_base::out |
  74. std::ios_base::binary | std::ios_base::trunc);
  75. else
  76. ofile.open("boost_sort_out.txt", std::ios_base::out |
  77. std::ios_base::binary | std::ios_base::trunc);
  78. if (ofile.good()) {
  79. for (unsigned v = 0; v < array.size(); ++v) {
  80. ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
  81. }
  82. ofile.close();
  83. }
  84. total += elapsed;
  85. array.clear();
  86. }
  87. if (stdSort)
  88. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  89. else
  90. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  91. return 0;
  92. }