Skip to content

Commit a3aecfe

Browse files
committed
Added examples, test, doxygen comments
1 parent d2c4d88 commit a3aecfe

7 files changed

Lines changed: 535 additions & 5 deletions

File tree

Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ exe alrbreaker : alrbreaker.cpp ;
3636
exe binaryalrbreaker : binaryalrbreaker.cpp ;
3737
exe caseinsensitive : caseinsensitive.cpp ;
3838
exe generalizedstruct : generalizedstruct.cpp ;
39+
exe timsort : timsortsample.cpp ;
3940

4041
# benchmarks need to be built with linkflags="-lboost_system -lboost_thread"
4142
#exe parallelint : parallelint.cpp boost_system ;

example/timsortalreadysorted.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
3+
Distributed under the Boost Software License, Version 1.0. (See
4+
accompanying file LICENSE_1_0.txt or copy at
5+
http://www.boost.org/LICENSE_1_0.txt)
6+
See http://www.boost.org/ for latest version.
7+
*/
8+
9+
#include <boost/sort/timsort.hpp>
10+
#include <time.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <algorithm>
14+
#include <vector>
15+
#include <string>
16+
#include <fstream>
17+
#include <sstream>
18+
#include <iostream>
19+
using namespace boost::sort;
20+
21+
#define DATA_TYPE int
22+
23+
24+
//Pass in an argument to test std::sort
25+
int main(int argc, const char ** argv) {
26+
size_t uCount,uSize=sizeof(DATA_TYPE);
27+
bool stdSort = false;
28+
unsigned loopCount = 1;
29+
for (int u = 1; u < argc; ++u) {
30+
if (std::string(argv[u]) == "-std")
31+
stdSort = true;
32+
else
33+
loopCount = atoi(argv[u]);
34+
}
35+
//Sorts the data once, then times sorting of already-sorted data
36+
loopCount += 1;
37+
std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
38+
if (input.fail()) {
39+
printf("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+
input.seekg (0, std::ios_base::beg);
48+
//Conversion to a vector
49+
array.resize(uCount);
50+
unsigned v = 0;
51+
while (input.good() && v < uCount) // EOF or failure stops the reading
52+
input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
53+
//Run multiple loops, if requested
54+
for (unsigned u = 0; u < loopCount; ++u) {
55+
clock_t start, end;
56+
double elapsed;
57+
start = clock();
58+
if (stdSort)
59+
//std::sort(&(array[0]), &(array[0]) + uCount);
60+
std::sort(array.begin(), array.end());
61+
else {
62+
printf("call\n");
63+
//integer_sort(&(array[0]), &(array[0]) + uCount);
64+
timsort(array.begin(), array.end());
65+
}
66+
end = clock();
67+
elapsed = static_cast<double>(end - start) ;
68+
std::ofstream ofile;
69+
if (stdSort)
70+
ofile.open("standard_sort_out.txt", std::ios_base::out |
71+
std::ios_base::binary | std::ios_base::trunc);
72+
else
73+
ofile.open("boost_sort_out.txt", std::ios_base::out |
74+
std::ios_base::binary | std::ios_base::trunc);
75+
if (ofile.good()) {
76+
for (unsigned v = 0; v < array.size(); ++v) {
77+
ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
78+
}
79+
ofile.close();
80+
}
81+
if (u)
82+
total += elapsed;
83+
}
84+
if (stdSort)
85+
printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
86+
else
87+
printf("timsort elapsed time %f\n", total / CLOCKS_PER_SEC);
88+
return 0;
89+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
3+
Distributed under the Boost Software License, Version 1.0. (See
4+
accompanying file LICENSE_1_0.txt or copy at
5+
http://www.boost.org/LICENSE_1_0.txt)
6+
See http://www.boost.org/ for latest version.
7+
*/
8+
9+
#include <boost/sort/timsort.hpp>
10+
#include <time.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <algorithm>
14+
#include <vector>
15+
#include <string>
16+
#include <fstream>
17+
#include <sstream>
18+
#include <iostream>
19+
using namespace boost::sort;
20+
21+
#define DATA_TYPE int
22+
23+
unsigned
24+
get_index(unsigned count)
25+
{
26+
unsigned result = unsigned((rand() % (1 << 16))*uint64_t(count)/(1 << 16));
27+
if (result >= count || result < 0)
28+
result = count - 1;
29+
return result;
30+
}
31+
32+
//Pass in an argument to test std::sort
33+
int main(int argc, const char ** argv) {
34+
srand(1);
35+
size_t uCount,uSize=sizeof(DATA_TYPE);
36+
bool stdSort = false;
37+
unsigned loopCount = 1;
38+
for (int u = 1; u < argc; ++u) {
39+
if (std::string(argv[u]) == "-std")
40+
stdSort = true;
41+
else
42+
loopCount = atoi(argv[u]);
43+
}
44+
//Sorts the data once, then times sorting of already-sorted data
45+
loopCount += 1;
46+
std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
47+
if (input.fail()) {
48+
printf("input.txt could not be opened\n");
49+
return 1;
50+
}
51+
double total = 0.0;
52+
std::vector<DATA_TYPE> array;
53+
input.seekg (0, std::ios_base::end);
54+
size_t length = input.tellg();
55+
uCount = length/uSize;
56+
input.seekg (0, std::ios_base::beg);
57+
//Conversion to a vector
58+
array.resize(uCount);
59+
unsigned v = 0;
60+
while (input.good() && v < uCount) // EOF or failure stops the reading
61+
input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
62+
//Run multiple loops, if requested
63+
for (unsigned u = 0; u < loopCount; ++u) {
64+
for (unsigned v = 0; v < uCount/10; ++v)
65+
std::swap(array[get_index(uCount)], array[get_index(uCount)]);
66+
clock_t start, end;
67+
double elapsed;
68+
start = clock();
69+
if (stdSort)
70+
//std::sort(&(array[0]), &(array[0]) + uCount);
71+
std::sort(array.begin(), array.end());
72+
else
73+
//integer_sort(&(array[0]), &(array[0]) + uCount);
74+
timsort(array.begin(), array.end());
75+
end = clock();
76+
elapsed = static_cast<double>(end - start) ;
77+
std::ofstream ofile;
78+
if (stdSort)
79+
ofile.open("standard_sort_out.txt", std::ios_base::out |
80+
std::ios_base::binary | std::ios_base::trunc);
81+
else
82+
ofile.open("boost_sort_out.txt", std::ios_base::out |
83+
std::ios_base::binary | std::ios_base::trunc);
84+
if (ofile.good()) {
85+
for (unsigned v = 0; v < array.size(); ++v) {
86+
ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
87+
}
88+
ofile.close();
89+
}
90+
if (u)
91+
total += elapsed;
92+
}
93+
if (stdSort)
94+
printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
95+
else
96+
printf("timsort elapsed time %f\n", total / CLOCKS_PER_SEC);
97+
return 0;
98+
}
99+

example/timsortsample.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
3+
Distributed under the Boost Software License, Version 1.0. (See
4+
accompanying file LICENSE_1_0.txt or copy at
5+
http://www.boost.org/LICENSE_1_0.txt)
6+
See http://www.boost.org/ for latest version.
7+
*/
8+
9+
#include <boost/sort/timsort.hpp>
10+
#include <time.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <algorithm>
14+
#include <vector>
15+
#include <string>
16+
#include <fstream>
17+
#include <sstream>
18+
#include <iostream>
19+
using namespace boost::sort;
20+
21+
#define DATA_TYPE int
22+
23+
24+
//Pass in an argument to test std::sort
25+
int main(int argc, const char ** argv)
26+
{
27+
size_t uCount, uSize = sizeof(DATA_TYPE);
28+
bool stdSort = false;
29+
unsigned loopCount = 1;
30+
for (int u = 1; u < argc; ++u)
31+
{
32+
if (std::string(argv[u]) == "-std")
33+
stdSort = true;
34+
else
35+
loopCount = atoi(argv[u]);
36+
}
37+
std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
38+
if (input.fail())
39+
{
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+
{
51+
input.seekg (0, std::ios_base::beg);
52+
//Conversion to a vector
53+
array.resize(uCount);
54+
unsigned v = 0;
55+
while (input.good() && v < uCount)
56+
input.read(reinterpret_cast<char *>(&(array[v++])), uSize );
57+
if (v < uCount)
58+
array.resize(v);
59+
clock_t start, end;
60+
double elapsed;
61+
start = clock();
62+
if (stdSort)
63+
std::sort(array.begin(), array.end());
64+
else
65+
boost::sort::timsort(array.begin(), array.end());
66+
end = clock();
67+
elapsed = static_cast<double>(end - start);
68+
std::ofstream ofile;
69+
if (stdSort)
70+
ofile.open("standard_sort_out.txt", std::ios_base::out |
71+
std::ios_base::binary | std::ios_base::trunc);
72+
else
73+
ofile.open("boost_sort_out.txt", std::ios_base::out |
74+
std::ios_base::binary | std::ios_base::trunc);
75+
if (ofile.good())
76+
{
77+
for (unsigned v = 0; v < array.size(); ++v)
78+
{
79+
ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
80+
}
81+
ofile.close();
82+
}
83+
total += elapsed;
84+
array.clear();
85+
}
86+
input.close();
87+
if (stdSort)
88+
printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
89+
else
90+
printf("timsort elapsed time %f\n", total / CLOCKS_PER_SEC);
91+
return 0;
92+
}

0 commit comments

Comments
 (0)