-
Notifications
You must be signed in to change notification settings - Fork 19
/
chash_test.cpp
144 lines (132 loc) · 3.98 KB
/
chash_test.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
#define _SCL_SECURE_NO_WARNINGS
#include "chash_map.h"
#include "chash_set.h"
#include <chrono>
#include <iostream>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <string>
#define assert(exp) assert_proc(exp, #exp, __FILE__, __LINE__)
auto assert_proc = [](bool no_error, char const *query, char const *file, size_t line)
{
if(!no_error)
{
struct hasher
{
size_t operator()(std::tuple<char const *, char const *, size_t> const &ref) const
{
return
std::hash<std::uintptr_t>()(reinterpret_cast<std::uintptr_t>(std::get<0>(ref))) ^
std::hash<std::uintptr_t>()(reinterpret_cast<std::uintptr_t>(std::get<1>(ref))) ^
std::hash<size_t>()(std::get<2>(ref));
}
};
static chash_set<std::tuple<char const *, char const *, size_t>, hasher> check;
if(check.emplace(query, file, line).second)
{
printf("%s(%zd):%s\n", file, line, query);
}
}
};
int main()
{
[&]
{
chash_multiset<int> ch =
{
1, 2, 3, 5, 3, 3, 4, 5, 6, 7, 1, 2, 3, 4
};
auto range = ch.equal_range(3);
assert(std::distance(range.first, range.second) == 4);
}();
std::unordered_map<int, int> xh;
chash_map<int, int> ch;
auto t = std::chrono::high_resolution_clock::now;
std::mt19937 mt(0);
auto mtr = std::uniform_int_distribution<int>(-10000000, 0);
std::vector<int> v;
v.resize(10000000);
auto reset = [&mtr, &mt, &v]()
{
for(auto &value : v)
{
value = mtr(mt);
}
};
assert(false);
auto testch = [&mtr, &mt, &c = ch, &v]()
{
for(int i = 0; i < int(v.size()); ++i)
{
c.insert(std::make_pair(v[i], i));
}
for(int i = 0; i < int(v.size()); ++i)
{
c.insert(std::make_pair(i, i));
}
for(int i = 0; i < int(v.size()); ++i)
{
c.erase(v[i]);
}
c.clear();
};
auto testxh = [&mtr, &mt, &c = xh, &v]()
{
for(int i = 0; i < int(v.size()); ++i)
{
c.insert(std::make_pair(v[i], i));
}
for(int i = 0; i < int(v.size()); ++i)
{
c.insert(std::make_pair(i, i));
}
for(int i = 0; i < int(v.size()); ++i)
{
c.erase(v[i]);
}
c.clear();
};
reset();
xh.max_load_factor(1);
ch.max_load_factor(1);
auto cs1 = t();
testch();
auto ce1 = t();
std::cout << "ch time 1(ms) = " << std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(ce1 - cs1).count() << std::endl;
auto xs1 = t();
testxh();
auto xr1 = t();
std::cout << "xh time 1(ms) = " << std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(xr1 - xs1).count() << std::endl;
reset();
xh.max_load_factor(0.8f);
ch.max_load_factor(0.8f);
auto cs2 = t();
testch();
auto ce2 = t();
std::cout << "ch time 2(ms) = " << std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(ce2 - cs2).count() << std::endl;
auto xs2 = t();
testxh();
auto xr2 = t();
std::cout << "xh time 2(ms) = " << std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(xr2 - xs2).count() << std::endl;
reset();
xh.max_load_factor(0.5f);
ch.max_load_factor(0.5f);
auto cs3 = t();
testch();
auto ce3 = t();
std::cout << "ch time 3(ms) = " << std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(ce3 - cs3).count() << std::endl;
auto xs3 = t();
testxh();
auto xr3 = t();
std::cout << "xh time 3(ms) = " << std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(xr3 - xs3).count() << std::endl;
v.clear();
system("pause");
//for(int i = 0; i < 20000000; ++i)
//{
// ch.insert(std::make_pair(mtr(mt), i));
//}
//ch.print_tree();
}