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