keyplusdatasample.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // spreadsort key and data 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. struct DATA_TYPE {
  21. int key;
  22. std::string data;
  23. };
  24. //functor example
  25. struct lessthan {
  26. inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {
  27. return x.key < y.key;
  28. }
  29. };
  30. struct rightshift {
  31. inline int operator()(const DATA_TYPE &x, const unsigned offset) {
  32. return x.key >> offset;
  33. }
  34. };
  35. //Pass in an argument to test std::sort
  36. int main(int argc, const char ** argv) {
  37. size_t uSize = sizeof(int);
  38. bool stdSort = false;
  39. unsigned loopCount = 1;
  40. for (int u = 1; u < argc; ++u) {
  41. if (std::string(argv[u]) == "-std")
  42. stdSort = true;
  43. else
  44. loopCount = atoi(argv[u]);
  45. }
  46. std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
  47. if (input.fail()) {
  48. printf("input.txt could not be opened\n");
  49. return 1;
  50. }
  51. input.seekg (0, std::ios_base::end);
  52. size_t length = input.tellg();
  53. double total = 0.0;
  54. std::vector<DATA_TYPE> array;
  55. array.reserve(length/uSize);
  56. unsigned uCount = length/uSize;
  57. //Run multiple loops, if requested
  58. for (unsigned u = 0; u < loopCount; ++u) {
  59. input.seekg (0, std::ios_base::beg);
  60. unsigned v = 0;
  61. while (input.good() && v++ < uCount) { // EOF or failure stops the reading
  62. DATA_TYPE element;
  63. input.read(reinterpret_cast<char *>(&(element.key)), sizeof(element.key));
  64. std::stringstream intstr;
  65. intstr << element.key;
  66. element.data = intstr.str();
  67. array.push_back(element);
  68. }
  69. clock_t start, end;
  70. double elapsed;
  71. start = clock();
  72. if (stdSort)
  73. std::sort(array.begin(), array.end(), lessthan());
  74. else
  75. integer_sort(array.begin(), array.end(), rightshift(), lessthan());
  76. end = clock();
  77. elapsed = static_cast<double>(end - start) ;
  78. std::ofstream ofile;
  79. if (stdSort)
  80. ofile.open("standard_sort_out.txt", std::ios_base::out |
  81. std::ios_base::binary | std::ios_base::trunc);
  82. else
  83. ofile.open("boost_sort_out.txt", std::ios_base::out |
  84. std::ios_base::binary | std::ios_base::trunc);
  85. if (ofile.good()) {
  86. for (unsigned v = 0; v < array.size(); ++v) {
  87. ofile.write(reinterpret_cast<char *>(&(array[v].key)),
  88. sizeof(array[v].key));
  89. ofile << array[v].data;
  90. }
  91. ofile.close();
  92. }
  93. total += elapsed;
  94. array.clear();
  95. }
  96. input.close();
  97. if (stdSort)
  98. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  99. else
  100. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  101. return 0;
  102. }