stringsample.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // spreadsort string 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/string_sort.hpp>
  10. #include <time.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <string>
  18. using std::string;
  19. using namespace boost::sort::spreadsort;
  20. #define DATA_TYPE string
  21. //Pass in an argument to test std::sort
  22. int main(int argc, const char ** argv) {
  23. std::ifstream indata;
  24. std::ofstream outfile;
  25. bool stdSort = false;
  26. unsigned loopCount = 1;
  27. for (int u = 1; u < argc; ++u) {
  28. if (std::string(argv[u]) == "-std")
  29. stdSort = true;
  30. else
  31. loopCount = atoi(argv[u]);
  32. }
  33. double total = 0.0;
  34. //Run multiple loops, if requested
  35. std::vector<DATA_TYPE> array;
  36. for (unsigned u = 0; u < loopCount; ++u) {
  37. indata.open("input.txt", std::ios_base::in | std::ios_base::binary);
  38. if (indata.bad()) {
  39. printf("input.txt could not be opened\n");
  40. return 1;
  41. }
  42. DATA_TYPE inval;
  43. while (!indata.eof() ) {
  44. indata >> inval;
  45. array.push_back(inval);
  46. }
  47. indata.close();
  48. clock_t start, end;
  49. double elapsed;
  50. start = clock();
  51. if (stdSort)
  52. //std::sort(&(array[0]), &(array[0]) + uCount);
  53. std::sort(array.begin(), array.end());
  54. else
  55. //string_sort(&(array[0]), &(array[0]) + uCount);
  56. string_sort(array.begin(), array.end());
  57. end = clock();
  58. elapsed = static_cast<double>(end - start);
  59. if (stdSort)
  60. outfile.open("standard_sort_out.txt", std::ios_base::out |
  61. std::ios_base::binary | std::ios_base::trunc);
  62. else
  63. outfile.open("boost_sort_out.txt", std::ios_base::out |
  64. std::ios_base::binary | std::ios_base::trunc);
  65. if (outfile.good()) {
  66. for (unsigned u = 0; u < array.size(); ++u)
  67. outfile << array[u] << "\n";
  68. outfile.close();
  69. }
  70. total += elapsed;
  71. array.clear();
  72. }
  73. if (stdSort)
  74. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  75. else
  76. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  77. return 0;
  78. }