]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/keyplusdatasample.cpp
1 // spreadsort key and data sorting example.
3 // Copyright Steven Ross 2009-2014.
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)
9 // See http://www.boost.org/libs/sort for library home page.
11 #include <boost/sort/spreadsort/spreadsort.hpp>
21 using namespace boost::sort::spreadsort
;
29 inline bool operator()(const DATA_TYPE
&x
, const DATA_TYPE
&y
) const {
35 inline int operator()(const DATA_TYPE
&x
, const unsigned offset
) {
36 return x
.key
>> offset
;
40 //Pass in an argument to test std::sort
41 int main(int argc
, const char ** argv
) {
42 size_t uSize
= sizeof(int);
44 unsigned loopCount
= 1;
45 for (int u
= 1; u
< argc
; ++u
) {
46 if (std::string(argv
[u
]) == "-std")
49 loopCount
= atoi(argv
[u
]);
51 std::ifstream
input("input.txt", std::ios_base::in
| std::ios_base::binary
);
53 printf("input.txt could not be opened\n");
56 input
.seekg (0, std::ios_base::end
);
57 size_t length
= input
.tellg();
59 std::vector
<DATA_TYPE
> array
;
60 array
.reserve(length
/uSize
);
61 unsigned uCount
= length
/uSize
;
62 //Run multiple loops, if requested
63 for (unsigned u
= 0; u
< loopCount
; ++u
) {
64 input
.seekg (0, std::ios_base::beg
);
66 while (input
.good() && v
++ < uCount
) { // EOF or failure stops the reading
68 input
.read(reinterpret_cast<char *>(&(element
.key
)), sizeof(element
.key
));
69 std::stringstream intstr
;
70 intstr
<< element
.key
;
71 element
.data
= intstr
.str();
72 array
.push_back(element
);
78 std::sort(array
.begin(), array
.end(), lessthan());
80 integer_sort(array
.begin(), array
.end(), rightshift(), lessthan());
82 elapsed
= static_cast<double>(end
- start
) ;
85 ofile
.open("standard_sort_out.txt", std::ios_base::out
|
86 std::ios_base::binary
| std::ios_base::trunc
);
88 ofile
.open("boost_sort_out.txt", std::ios_base::out
|
89 std::ios_base::binary
| std::ios_base::trunc
);
91 for (unsigned v
= 0; v
< array
.size(); ++v
) {
92 ofile
.write(reinterpret_cast<char *>(&(array
[v
].key
)),
93 sizeof(array
[v
].key
));
94 ofile
<< array
[v
].data
;
103 printf("std::sort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);
105 printf("spreadsort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);