1 // spreadsort on a mostly sorted array 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/spreadsort.hpp>
12 #include <time.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <algorithm>
16 #include <vector>
17 #include <string>
18 #include <fstream>
19 #include <sstream>
20 #include <iostream>
21 using namespace boost::sort::spreadsort;
22
23 #define DATA_TYPE int
24
25 unsigned
get_index(unsigned count)26 get_index(unsigned count)
27 {
28 unsigned result = unsigned((rand() % (1 << 16))*uint64_t(count)/(1 << 16));
29 if (result >= count || result < 0)
30 result = count - 1;
31 return result;
32 }
33
34 //Pass in an argument to test std::sort
main(int argc,const char ** argv)35 int main(int argc, const char ** argv) {
36 srand(1);
37 size_t uCount,uSize=sizeof(DATA_TYPE);
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 //Sorts the data once, then times sorting of already-sorted data
47 loopCount += 1;
48 std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
49 if (input.fail()) {
50 printf("input.txt could not be opened\n");
51 return 1;
52 }
53 double total = 0.0;
54 std::vector<DATA_TYPE> array;
55 input.seekg (0, std::ios_base::end);
56 size_t length = input.tellg();
57 uCount = length/uSize;
58 input.seekg (0, std::ios_base::beg);
59 //Conversion to a vector
60 array.resize(uCount);
61 unsigned v = 0;
62 while (input.good() && v < uCount) // EOF or failure stops the reading
63 input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
64 //Run multiple loops, if requested
65 for (unsigned u = 0; u < loopCount; ++u) {
66 for (unsigned v = 0; v < uCount/10; ++v)
67 std::swap(array[get_index(uCount)], array[get_index(uCount)]);
68 clock_t start, end;
69 double elapsed;
70 start = clock();
71 if (stdSort)
72 //std::sort(&(array[0]), &(array[0]) + uCount);
73 std::sort(array.begin(), array.end());
74 else
75 //integer_sort(&(array[0]), &(array[0]) + uCount);
76 integer_sort(array.begin(), array.end());
77 end = clock();
78 elapsed = static_cast<double>(end - start) ;
79 std::ofstream ofile;
80 if (stdSort)
81 ofile.open("standard_sort_out.txt", std::ios_base::out |
82 std::ios_base::binary | std::ios_base::trunc);
83 else
84 ofile.open("boost_sort_out.txt", std::ios_base::out |
85 std::ios_base::binary | std::ios_base::trunc);
86 if (ofile.good()) {
87 for (unsigned v = 0; v < array.size(); ++v) {
88 ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
89 }
90 ofile.close();
91 }
92 if (u)
93 total += elapsed;
94 }
95 if (stdSort)
96 printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
97 else
98 printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
99 return 0;
100 }
101