• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // spreadsort fully sorted data 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 
26 //Pass in an argument to test std::sort
main(int argc,const char ** argv)27 int 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(argv[u]);
36   }
37   //Sorts the data once, then times sorting of already-sorted data
38   loopCount += 1;
39   std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
40   if (input.fail()) {
41     printf("input.txt could not be opened\n");
42     return 1;
43   }
44   double total = 0.0;
45   std::vector<DATA_TYPE> array;
46   input.seekg (0, std::ios_base::end);
47     size_t length = input.tellg();
48   uCount = length/uSize;
49   input.seekg (0, std::ios_base::beg);
50   //Conversion to a vector
51   array.resize(uCount);
52   unsigned v = 0;
53   while (input.good() && v < uCount) // EOF or failure stops the reading
54     input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
55   //Run multiple loops, if requested
56   for (unsigned u = 0; u < loopCount; ++u) {
57     clock_t start, end;
58     double elapsed;
59     start = clock();
60     if (stdSort)
61       //std::sort(&(array[0]), &(array[0]) + uCount);
62       std::sort(array.begin(), array.end());
63     else {
64       printf("call\n");
65       //integer_sort(&(array[0]), &(array[0]) + uCount);
66       integer_sort(array.begin(), array.end());
67     }
68     end = clock();
69     elapsed = static_cast<double>(end - start) ;
70     std::ofstream ofile;
71     if (stdSort)
72       ofile.open("standard_sort_out.txt", std::ios_base::out |
73                  std::ios_base::binary | std::ios_base::trunc);
74     else
75       ofile.open("boost_sort_out.txt", std::ios_base::out |
76                  std::ios_base::binary | std::ios_base::trunc);
77     if (ofile.good()) {
78       for (unsigned v = 0; v < array.size(); ++v) {
79         ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
80       }
81       ofile.close();
82     }
83     if (u)
84       total += elapsed;
85   }
86   if (stdSort)
87     printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
88   else
89     printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
90   return 0;
91 }
92