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