forked from philsquared/hash_trie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_main.cpp
181 lines (145 loc) · 4.81 KB
/
bench_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "hash_trie.hpp"
#include <vector>
#include <set>
#include <unordered_set>
#include <nonius/nonius_single.h++>
size_t num = 1000000;
std::vector<std::string> vector_strings;
std::vector<int> vector_ints;
std::vector<size_t> vector_hashes;
hamt::hash_trie<int> hamt_ints;
hamt::hash_trie<std::string> hamt_strings;
std::set<std::string> set_strings;
std::set<int> set_ints;
std::unordered_set<std::string> unordered_set_strings;
std::unordered_set<int> unordered_set_ints;
template<typename Container, typename Iterator>
bool isEnd( Iterator const& it, Container const& container ) {
return it == container.end();
}
template<typename T, typename Iterator>
bool isEnd( Iterator const& it, hamt::hash_trie<T> const& ) {
return !it.leaf();
}
int main(int argc, char * argv[]) {
std::cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
if( argc > 2 && std::string(argv[argc-2]) == "-i" ) {
std::stringstream ss;
ss << argv[argc-1];
ss >> num;
if( ss.fail() ) {
std::cerr << "[" << argv[argc-1] << "] is not a number" << std::endl;
return -1;
}
std::cout << "Using custom size of: " << num << std::endl;
argc -= 2;
}
else
std::cout << "Using default size of: " << num << std::endl;
vector_strings.reserve(num);
vector_ints.reserve(num);
vector_hashes.reserve(num);
std::cout << "Setting up ";
std::cout.flush();
std::string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-";
auto len = static_cast<size_t>( std::ceil(std::log(num)/std::log(64)) );
std::vector<size_t> indices(num, 0);
std::string str(len, '-');
for( size_t i =0; i < num; ++i ) {
if( i % (num/10) == 0 ) {
std::cout << ".";
std::cout.flush();
}
bool incDigit = true;
for( size_t pos = 0; pos < len; ++pos ) {
str[pos] = chars[indices[pos]];
if( incDigit ) {
if( ++indices[pos] == 64 )
indices[pos] = 0;
else
incDigit = false;
}
}
vector_ints.push_back(i);
hamt_ints.insert(i);
set_ints.insert(i);
unordered_set_ints.insert(i);
vector_strings.push_back( str );
hamt_strings.insert( str );
set_strings.insert( str );
unordered_set_strings.insert( str );
vector_hashes.push_back( std::hash<std::string>()( str ) );
}
std::cout << " completed" << std::endl;
std::cout << ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ." << std::endl;
nonius::main(argc, argv);
}
template<typename Src, typename Dest>
void testFind( Src const& src, Dest& dest ) {
int count = 0;
const int iterations = 10;
for( int j = 0; j < iterations; ++j ) {
for (auto const &item : src)
count += isEnd(dest.find(item), dest) ? 0 : 1;
}
if( count < 0 )
throw std::domain_error("wrong count: " + std::to_string( count ) );
}
NONIUS_BENCHMARK("hash_trie<int>::insert", []{
hamt::hash_trie<int> ints;
for( int i =0; i < num; ++i )
ints.insert( i );
});
NONIUS_BENCHMARK("set<int>::insert", []{
std::set<int> ints;
for( int i =0; i < num; ++i )
ints.insert( i );
});
NONIUS_BENCHMARK("unordered_set<int>::insert", []{
std::unordered_set<int> ints;
for( int i =0; i < num; ++i )
unordered_set_ints.insert( i );
});
NONIUS_BENCHMARK("hash_trie<int>::find", []{
testFind( vector_ints, hamt_ints );
});
NONIUS_BENCHMARK("set<int>::find", []{
testFind( vector_ints, set_ints );
});
NONIUS_BENCHMARK("unordered_set<int>::find", []{
testFind( vector_ints, unordered_set_ints );
});
NONIUS_BENCHMARK("hash_trie<hash>::find", []{
testFind( vector_hashes, hamt_ints );
});
NONIUS_BENCHMARK("set<hash>::find", []{
testFind( vector_hashes, set_ints );
});
NONIUS_BENCHMARK("unordered_set<hash>::find", []{
testFind( vector_hashes, unordered_set_ints );
});
NONIUS_BENCHMARK("hash_trie<string>::find", []{
testFind( vector_strings, hamt_strings );
});
NONIUS_BENCHMARK("set<string>::find", []{
testFind( vector_strings, set_strings );
});
NONIUS_BENCHMARK("unordered_set<string>::find", []{
testFind( vector_strings, unordered_set_strings );
});
NONIUS_BENCHMARK("count_set_bits", []{
int totals = 0;
for( auto hash : vector_hashes ) {
totals += hamt::detail::count_set_bits( hash );
}
if( totals == 0 )
throw std::logic_error( "zero" );
});
NONIUS_BENCHMARK("count_set_bits_popcount", []{
int totals = 0;
for( auto hash : vector_hashes ) {
totals += hamt::detail::count_set_bits_popcount( hash );
}
if( totals == 0 )
throw std::logic_error( "zero" );
});