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