-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtest_avlmap.cpp
214 lines (178 loc) · 4.73 KB
/
test_avlmap.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//=====================================================================
//
// test_avlmap.cpp - test avl_hash_map
//
// Created by skywind on 2017/12/08
// Last change: 2017/12/08 16:48:39
//
//=====================================================================
#include "avlhash.h"
#include "avlhash.c"
#include "avlmini.c"
#include "test_avl.h"
// open this for collision test
#if 0
#define SAME_HASH
#endif
#ifndef SAME_HASH
struct NodeHash { size_t operator()(const int& rhs) const { return (size_t)rhs; } };
#else
struct NodeHash { size_t operator()(const int& rhs) const { return 0; } };
#endif
#ifdef _MSC_VER
#include <unordered_map>
typedef std::unordered_map<int, int, NodeHash> map_type;
#elif (__GNUC__ <= 4) && (__GNUC_MINOR__ <= 7)
#include <tr1/memory>
#include <tr1/unordered_map>
typedef std::tr1::unordered_map<int, int, NodeHash> map_type;
#else
#include <memory>
#include <unordered_map>
typedef std::unordered_map<int, int, NodeHash> map_type;
#endif
int node_compare(const void *key1, const void *key2)
{
#ifndef SAME_HASH
size_t x = (size_t)key1;
size_t y = (size_t)key2;
return (int)(x - y);
#else
return 0;
#endif
}
size_t node_hash(const void *key)
{
return (size_t)key;
}
//---------------------------------------------------------------------
// benchmark
//---------------------------------------------------------------------
void benchmark(const char *name, int mode, int count)
{
int *keys = new int[count * 2];
int *search = keys + count;
struct avl_hash_map hmap;
map_type umap;
unsigned int ts;
printf("benchmark %s:\n", name);
random_keys(keys, count, 0x11223344);
random_keys(search, count, 0x55667788);
avl_map_init(&hmap, node_hash, node_compare);
/* hmap.builtin = 10; */
/* printf("builtin=%d\n", hmap.builtin); */
/* avl_map_reserve(&hmap, count); */
#ifdef _MSC_VER
umap.reserve(count);
#elif __GNUC__ > 4
umap.reserve(count);
#endif
avl_map_reserve(&hmap, count);
printf("insert time: ");
sleepms(100);
ts = gettime();
if (mode == 0) {
for (int i = 0; i < count; i++) {
int key = keys[i];
avl_map_set(&hmap, (void*)key, (void*)(key * 10));
}
}
else if (mode == 1) {
for (int i = 0; i < count; i++) {
int key = keys[i];
umap[key] = key * 10;
}
}
ts = gettime() - ts;
printf("%dms\n", (int)ts);
/* printf("%d\n", itemnum); */
avl_map_reserve(&hmap, count);
printf("search time: ");
sleepms(100);
ts = gettime();
if (mode == 0) {
for (int i = 0; i < count; i++) {
int key = search[i];
avl_hash_entry *entry;
#ifdef NO_INLINE_TEMPLATE
// standard: call hash() / compare() with function pointer
entry = avl_map_find(&hmap, ((void*)key));
#else
// template: call hash() / compare() inline
avl_map_search(&hmap, ((void*)key), node_hash, node_compare, entry);
#endif
int val = (int)entry->value;
assert(entry);
if (val != key * 10) {
printf("key=%d val=%d\n", key, val);
printf("error \n");
return;
assert(val == key * 10);
}
}
}
else if (mode == 1) {
for (int i = 0; i < count; i++) {
int key = search[i];
map_type::iterator it = umap.find(key);
assert(it != umap.end());
assert(it->second == key * 10);
}
}
ts = gettime() - ts;
printf("%dms\n", (int)ts);
printf("delete time: ");
sleepms(100);
ts = gettime();
if (mode == 0) {
for (int i = 0; i < count; i++) {
int key = search[count - 1 - i];
int hr = avl_map_remove(&hmap, (void*)key);
assert(hr == 0);
}
}
else if (mode == 1) {
for (int i = 0; i < count; i++) {
int key = search[count - 1 - i];
map_type::iterator it = umap.find(key);
assert(it != umap.end());
umap.erase(it);
}
}
ts = gettime() - ts;
printf("%dms\n", (int)ts);
delete keys;
printf("\n");
}
//---------------------------------------------------------------------
// standard test
//---------------------------------------------------------------------
void test_standard()
{
#define TTIMES 10000000
benchmark("avl-hash", 0, TTIMES);
benchmark("unordered_map", 1, TTIMES);
}
//---------------------------------------------------------------------
// collision test
//---------------------------------------------------------------------
void test_collision()
{
for (int i = 1000; i <= 30000; i += 1000) {
printf("\n<%d>\n", i);
benchmark("avl-hash", 0, i);
benchmark("unordered_map", 1, i);
}
}
//---------------------------------------------------------------------
// program entry
//---------------------------------------------------------------------
int main(void)
{
#ifndef SAME_HASH
test_standard();
#else
test_collision();
#endif
return 0;
}