-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench_iteration.cpp
108 lines (93 loc) · 2.22 KB
/
bench_iteration.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
#include <iostream>
#include <unordered_map>
#include <cstdlib>
#include "index_map_for_iteration.h"
#include "timer.h"
struct Data {
float f1;
float f2;
float f3;
Data(): Data(0, 0, 0) {}
Data(float _f1, float _f2, float _f3) {
f1 = _f1;
f2 = _f2;
f3 = _f3;
}
};
static const int element_size = 100000000;
int test_index_map(index_map<uint64_t, Data> &m) {
{
Timer t("index_map::insert");
for (int i = 0; i < element_size; ++i) {
uint64_t key = ((uint64_t)rand() << 32) | rand();
m.insert(std::make_pair(key, Data(1.0f, 2.0f, 3.0f)));
}
}
{
Timer t("index_map::iteration");
int total = 0;
for (int i = 0; i < 10; ++i)
for (auto it : m) {
total += it.first + it.second.f1 + it.second.f2 + it.second.f3;
}
}
{
Timer t("index_map::find&erase");
for (int i = 0; i < 5000; ++i) {
uint64_t key = ((uint64_t)rand() << 32) | rand();
auto it = m.find(key);
if (it != m.end()) {
m.erase(it);
}
}
}
{
Timer t("index_map::[]");
for (int i = 0; i < 5000; ++i) {
uint64_t key = ((uint64_t)rand() << 32) | rand();
m[key] = Data();
}
}
}
int test_unordered_map(unordered_map<uint64_t, Data> &m) {
{
Timer t("unordered_map::insert");
for (int i = 0; i < element_size; ++i) {
uint64_t key = ((uint64_t)rand() << 32) | rand();
m.insert(std::make_pair(key, Data(1.0f, 2.0f, 3.0f)));
}
}
{
Timer t("unordered_map::iteration");
int total = 0;
for (int i = 0; i < 10; ++i)
for (auto it : m) {
total += it.first + it.second.f1 + it.second.f2 + it.second.f3;
}
}
{
Timer t("unordered_map::find&erase");
for (int i = 0; i < 5000; ++i) {
uint64_t key = ((uint64_t)rand() << 32) | rand();
auto it = m.find(key);
if (it != m.end()) {
m.erase(it);
}
}
}
{
Timer t("unordered_map::[]");
for (int i = 0; i < 5000; ++i) {
uint64_t key = ((uint64_t)rand() << 32) | rand();
m[key] = Data();
}
}
}
int main() {
index_map<uint64_t, Data> m1;
unordered_map<uint64_t, Data> m2;
srand(time(NULL));
test_index_map(m1);
cout << "--------------------------------" << endl;
test_unordered_map(m2);
}