-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdictionaries-implementation-benchmark.cpp
158 lines (140 loc) · 4.34 KB
/
dictionaries-implementation-benchmark.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
#include <algorithm>
#include <chrono>
#include <iostream>
#include <fstream>
#include <map>
#include <unordered_map>
#include <vector>
#include <stdlib.h>
#include <unistd.h>
class Timer {
public:
typedef std::chrono::system_clock clock_type;
Timer() :start_time_(clock_type::now()) {}
void reset() { start_time_ = clock_type::now(); }
long elapsed_ms() { return elapsed_count<std::chrono::milliseconds>(); }
long elapsed_us() { return elapsed_count<std::chrono::microseconds>(); }
long elapsed_ns() { return elapsed_count<std::chrono::nanoseconds>(); }
private:
std::chrono::time_point<std::chrono::system_clock> start_time_;
template <typename T>
inline int elapsed_count() {
return std::chrono::duration_cast<T>(
clock_type::now() - start_time_).count();
}
};
template <typename T>
void RunBench(const std::vector<std::pair<T, T> > &pairs, bool reserve) {
{
std::cout << "\nsorted list\n----------" << std::endl;
Timer timer;
std::vector<std::pair<T, T> > list = pairs;
std::sort(list.begin(), list.end());
std::cout << "create: " << timer.elapsed_ms() << std::endl;
timer.reset();
for (const auto &kv : pairs) {
std::binary_search(
list.cbegin(), list.cend(), kv,
[&](const std::pair<T, T> &a,
const std::pair<T, T> &b) { return a.first < b.first; });
}
std::cout << "search: " << timer.elapsed_ms() << std::endl;
}
{
std::cout << "\nmap\n----------" << std::endl;
Timer timer;
std::map<T, T> map;
for (const auto &kv : pairs) {
map.insert(kv);
}
std::cout << "create: " << timer.elapsed_ms() << std::endl;
timer.reset();
for (const auto &kv : pairs) {
map.find(kv.first);
}
std::cout << "search: " << timer.elapsed_ms() << std::endl;
}
{
std::cout << "\nhash\n----------" << std::endl;
Timer timer;
std::unordered_map<T, T> hash;
if (reserve) {
hash.reserve(pairs.size());
}
for (const auto &kv : pairs) {
hash.insert(kv);
}
std::cout << "create: " << timer.elapsed_ms() << std::endl;
timer.reset();
for (const auto &kv : pairs) {
hash.find(kv.first);
}
std::cout << "search: " << timer.elapsed_ms() << "\n\n";
std::cout << "bucket count = " << hash.bucket_count() << "\n";
std::cout << "load factor = " << hash.load_factor() << "\n";
std::cout << "max load factor = " << hash.max_load_factor() << "\n";
}
}
int main(int argc, char **argv) {
int c;
bool use_string = false;
bool reserve = false;
std::size_t num = 1000000;
while ((c = getopt(argc, argv, "hrsn:")) != -1) {
switch (c) {
case 'h':
std::cout << "usage: " << argv[0] << " [-h] [-r] [-x] [-n num]\n";
return 0;
break;
case 'n':
num = static_cast<std::size_t>(atoi(optarg));
break;
case 'r':
reserve = true;
break;
case 's':
use_string = true;
break;
default:
std::cout << "usage: " << argv[0] << " [-h] [-r] [-n num]\n";
return 1;
}
}
std::cout << "generating list with " << num
<< (use_string ? " string " : " integer ")
<< "pairs..." << std::endl;
std::ifstream urandom("/dev/urandom",
std::ifstream::in | std::ifstream::binary);
Timer gen_timer;
if (use_string) {
std::vector<std::pair<std::string, std::string> > pairs;
if (reserve) {
pairs.reserve(num);
}
char charbuf[16];
for (std::size_t i = 0; i < num; i++) {
urandom.read(charbuf, sizeof(charbuf));
pairs.emplace_back(std::make_pair(std::string(charbuf, 8),
std::string(charbuf + 8, 8)));
}
std::cout << "generated in " << gen_timer.elapsed_ms() << " ms\n";
RunBench<std::string>(pairs, reserve);
} else {
std::vector<std::pair<int, int> > pairs;
if (reserve) {
pairs.reserve(num);
}
char charbuf[sizeof(int)];
for (std::size_t i = 0; i < num; i++) {
int key, val;
urandom.read(charbuf, sizeof(charbuf));
key = *reinterpret_cast<int*>(charbuf);
urandom.read(charbuf, sizeof(charbuf));
val = *reinterpret_cast<int*>(charbuf);
pairs.emplace_back(std::make_pair(key, val));
}
std::cout << "generated in " << gen_timer.elapsed_ms() << " ms\n";
RunBench<int>(pairs, reserve);
}
return 0;
}