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

source code of boost/libs/sort/example/sample.cpp