-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
64 lines (53 loc) · 1.84 KB
/
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
#include <algorithm>
#include <cassert>
#include <chrono>
#include <iostream>
#include <vector>
#include "louds-patricia.hpp"
using namespace std;
using namespace std::chrono;
int main() {
ios_base::sync_with_stdio(false);
vector<string> keys;
string line;
while (getline(cin, line)) {
keys.push_back(line);
}
high_resolution_clock::time_point begin = high_resolution_clock::now();
louds::Patricia patricia;
for (uint64_t i = 0; i < keys.size(); ++i) {
patricia.add(keys[i]);
}
patricia.build();
high_resolution_clock::time_point end = high_resolution_clock::now();
double elapsed = (double)duration_cast<nanoseconds>(end - begin).count();
cout << "build = " << (elapsed / keys.size()) << " ns/key" << endl;
cout << "#keys = " << patricia.n_keys() << endl;
cout << "#nodes = " << patricia.n_nodes() << endl;
cout << "size = " << patricia.size() << " bytes" << endl;
vector<int64_t> ids(keys.size());
begin = high_resolution_clock::now();
for (uint64_t i = 0; i < keys.size(); ++i) {
ids[i] = patricia.lookup(keys[i]);
assert(ids[i] != -1);
}
end = high_resolution_clock::now();
elapsed = (double)duration_cast<nanoseconds>(end - begin).count();
cout << "seq. lookup = " << (elapsed / keys.size()) << " ns/key" << endl;
sort(ids.begin(), ids.end());
for (uint64_t i = 0; i < ids.size(); ++i) {
if (i != (uint64_t)ids[i]) {
cout << "i = " << i << ", ids[i] = " << ids[i] << endl;
}
assert((uint64_t)ids[i] == i);
}
random_shuffle(keys.begin(), keys.end());
begin = high_resolution_clock::now();
for (uint64_t i = 0; i < keys.size(); ++i) {
assert(patricia.lookup(keys[i]) != -1);
}
end = high_resolution_clock::now();
elapsed = (double)duration_cast<nanoseconds>(end - begin).count();
cout << "rnd. lookup = " << (elapsed / keys.size()) << " ns/key" << endl;
return 0;
}