1 // spreadsort 64-bit integer 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/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 //[int64bit_1
24 #define DATA_TYPE boost::int64_t
25 //] [/int64bit_1]
26
27 //Pass in an argument to test std::sort
main(int argc,const char ** argv)28 int main(int argc, const char ** argv) {
29 size_t uCount,uSize=sizeof(DATA_TYPE);
30 bool stdSort = false;
31 unsigned loopCount = 1;
32 for (int u = 1; u < argc; ++u) {
33 if (std::string(argv[u]) == "-std")
34 stdSort = true;
35 else
36 loopCount = atoi(argv[u]);
37 }
38 std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
39 if (input.fail()) {
40 printf("input.txt could not be opened\n");
41 return 1;
42 }
43 double total = 0.0;
44 std::vector<DATA_TYPE> array;
45 input.seekg (0, std::ios_base::end);
46 size_t length = input.tellg();
47 uCount = length/uSize;
48 //Run multiple loops, if requested
49 for (unsigned u = 0; u < loopCount; ++u) {
50 input.seekg (0, std::ios_base::beg);
51 //Conversion to a vector
52 array.resize(uCount);
53 unsigned v = 0;
54 while (input.good() && v < uCount)
55 input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
56 if (v < uCount)
57 array.resize(v);
58 clock_t start, end;
59 double elapsed;
60 start = clock();
61 if (stdSort)
62 //std::sort(&(array[0]), &(array[0]) + uCount);
63 std::sort(array.begin(), array.end());
64 else
65 //boost::sort::spreadsort::spreadsort(&(array[0]), &(array[0]) + uCount);
66 //[int64bit_2
67 boost::sort::spreadsort::spreadsort(array.begin(), array.end());
68 //] [/int64bit_2]
69 end = clock();
70 elapsed = static_cast<double>(end - start) ;
71 std::ofstream ofile;
72 if (stdSort)
73 ofile.open("standard_sort_out.txt", std::ios_base::out |
74 std::ios_base::binary | std::ios_base::trunc);
75 else
76 ofile.open("boost_sort_out.txt", std::ios_base::out |
77 std::ios_base::binary | std::ios_base::trunc);
78 if (ofile.good()) {
79 for (unsigned v = 0; v < array.size(); ++v) {
80 ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
81 }
82 ofile.close();
83 }
84 total += elapsed;
85 array.clear();
86 }
87 input.close();
88 if (stdSort)
89 printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
90 else
91 printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
92 return 0;
93 }
94