各类map效率比较实用2(151)

1、果然大数据hash表虐红黑树了。


2、但是唯一确定的是unordered_map > hash_map > map,官方也是如此认为的,前面map虽然高效,但是开辟的内存却是更多,也更难使用。(见146使用方法)

3、使用数据,50000存储,2000查询

4、现象,map在上万的查询就各种内存泄露了,cmap依旧需要removeall释放。map性能可能更差,上千未测试map的数据;

下面是代码:++
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <afx.h>
#include <stdio.h>
#include <iostream>
#include <deque>
#include <string>
#include <queue>
#include <map>
#include <unordered_map>
#include <hash_map>
#include <xhash>
#include <vector>
#include <stdarg.h>
#include <afxtempl.h>
#include <functional>
#include <algorithm>
using namespace std;
typedef struct TDATA0 {
int year;
int month;
int day;
} *PTDATA0;
typedef struct TDATA1 {
TDATA1& operator= (TDATA1& t) {
name = t.name;
adress = t.adress;
parents = t.parents;
brithday = t.brithday;
return *this;
}
CString name;
CString adress;
CString parents;
TDATA0 brithday;
} *PTDATA1;
typedef struct FINDDATA {
FINDDATA(int in) {
index = in;
::CoCreateGuid(&findguid);
}
~FINDDATA() {}
operator int () {
return index;
}
int index;
GUID findguid;
} *PFINDDATA;
std::wstring GUIDToWstring(GUID* guid) {
wchar_t guid_string[37];
swprintf(
guid_string, sizeof(guid_string) / sizeof(guid_string[0]),
L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
guid->Data1, guid->Data2, guid->Data3,
guid->Data4[0], guid->Data4[1], guid->Data4[2],
guid->Data4[3], guid->Data4[4], guid->Data4[5],
guid->Data4[6], guid->Data4[7]);
return guid_string;
}
struct WCharLess : public binary_function<const wchar_t*, const wchar_t*, bool>
{
public:
result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right) const
{
return(_wcsicmp(_Left, _Right) < 0 ? true : false);
}
};
//struct KeyHasher
//{
// std::size_t operator()(const CString& k) const
// {
// using std::size_t;
// using std::tr1::hash;
//
// return (hash<CString>()(k));
// }
//};
typedef std::queue<FINDDATA> stru_find;
typedef std::map<CString, TDATA1> map_d_type;
typedef stdext::hash_map<CString, TDATA1, stdext::hash_compare<const wchar_t*, WCharLess>> hashmap_d_type;
typedef std::tr1::unordered_map<wstring, TDATA1> unordemap_d_type;
typedef std::vector<std::pair<CString, TDATA1>> vecpair_d_type;
typedef CMap<CString, LPCTSTR, TDATA1, TDATA1&> cmap_d_type;
void createfinddata(stru_find& find) {
for (int i = 2000; i < 4000; i++)
{
find.push(FINDDATA(i));
}
find.push(0);
}
size_t loadCmapData(stru_find find, cmap_d_type &test_data) {
for (int i = 1; i <= 50000; i++)
{
GUID guid;
wchar_t strbuffer[10] = {0};
_itow(i, strbuffer, 10);
TDATA1 t ;
t.name = _T("name");
t.name += strbuffer;
t.adress = _T("address");
t.adress += strbuffer;
t.parents = _T("parents");
t.parents += strbuffer;
t.brithday.year = t.brithday.month = t.brithday.day = i;
::CoCreateGuid(&guid);
if (i == find.front()) {
guid = find.front().findguid;
find.pop();
}
CString temp(GUIDToWstring(&guid).c_str());
test_data.SetAt(temp, t);
}
return test_data.GetSize();
}
void printfcmap(stru_find& find, cmap_d_type &test_data) {
TDATA1 out;
BOOL b;
CString temp;
int n = find.size();
for (int i = 0; i < n; i++)
{
temp.Empty();
temp.Append(GUIDToWstring(&find.front().findguid).c_str());
b = test_data.Lookup(temp, out);
if (b) {
wcout << endl << L"find count: " << find.size() << "find index:"<< find.front() << endl
<< "data key :" << temp.GetString() << endl
<< L"name :" << out.name.GetString()<< endl
<< L"address :"<< out.adress.GetString() <<endl
<< L"parents :"<<out.parents.GetString() << endl
<< L"birthday :" << out.brithday.year << " "<< out.brithday.month<<" "<<out.brithday.day << endl;
find.pop();
}
}
}
size_t loadMapData(stru_find findata, map_d_type& mapdata) {
for (int i = 1; i <= 50000; i++)
{
GUID guid;
wchar_t strbuffer[10] = {0};
_itow(i, strbuffer, 10);
TDATA1 t ;
t.name = _T("name");
t.name += strbuffer;
t.adress = _T("address");
t.adress += strbuffer;
t.parents = _T("parents");
t.parents += strbuffer;
t.brithday.year = t.brithday.month = t.brithday.day = i;
::CoCreateGuid(&guid);
if (i == findata.front()) {
guid = findata.front().findguid;
findata.pop();
}
CString temp(GUIDToWstring(&guid).c_str());
mapdata.insert(map_d_type::value_type(temp, t));
}
return mapdata.size();
}
void printfmap(stru_find& find, map_d_type &test_data) {
CString temp;
int n = find.size();
for (int i = 0; i < n; i++)
{
temp.Empty();
temp.Append(GUIDToWstring(&find.front().findguid).c_str());
map_d_type::iterator ifind = test_data.find(temp);
//map_d_type::iterator ifind ;
//out = (*ifind).second;
if (ifind != test_data.end()) {
wcout << endl << L"left find count: " << find.size() << " find index:"<< find.front() << endl
<< "data key :" << temp.GetString() << endl
<< L"name :" << (*ifind).second.name.GetString()<< endl
<< L"address :"<< (*ifind).second.adress.GetString() <<endl
<< L"parents :"<<(*ifind).second.parents.GetString() << endl
<< L"birthday :" << (*ifind).second.brithday.year << " "<< (*ifind).second.brithday.month<<" "<<(*ifind).second.brithday.day << endl;
find.pop();
}
}
}
size_t loadUnoderMapData(stru_find findata, unordemap_d_type& test_data) {
test_data.rehash(50000);
for (int i = 1; i <= 50000; i++)
{
GUID guid;
wchar_t strbuffer[10] = {0};
_itow(i, strbuffer, 10);
TDATA1 t ;
t.name = _T("name");
t.name += strbuffer;
t.adress = _T("address");
t.adress += strbuffer;
t.parents = _T("parents");
t.parents += strbuffer;
t.brithday.year = t.brithday.month = t.brithday.day = i;
::CoCreateGuid(&guid);
if (i == findata.front()) {
guid = findata.front().findguid;
findata.pop();
}
wstring temp(GUIDToWstring(&guid).c_str());
test_data.insert(unordemap_d_type::value_type(temp, t));
}
return test_data.size();
}
void printUnoderMap(stru_find findata, unordemap_d_type& test_data) {
wstring temp;
int n = findata.size();
for (int i = 0; i < n; i++)
{
//temp.empty();
temp = (GUIDToWstring(&findata.front().findguid));
unordemap_d_type::iterator ifind = test_data.find(temp);
if (ifind != test_data.end()) {
wcout << endl << L"left find count: " << findata.size() << " find index:"<< findata.front() << endl
<< "data key :" << temp << endl
<< L"name :" << (*ifind).second.name.GetString()<< endl
<< L"address :"<< (*ifind).second.adress.GetString() <<endl
<< L"parents :"<<(*ifind).second.parents.GetString() << endl
<< L"birthday :" << (*ifind).second.brithday.year << " "<< (*ifind).second.brithday.month<<" "<<(*ifind).second.brithday.day << endl;
findata.pop();
}
}
}
size_t loadHashMapData(stru_find findata, hashmap_d_type& test_data) {
test_data.rehash(50000);
for (int i = 1; i <= 50000; i++)
{
GUID guid;
wchar_t strbuffer[10] = {0};
_itow(i, strbuffer, 10);
TDATA1 t ;
t.name = _T("name");
t.name += strbuffer;
t.adress = _T("address");
t.adress += strbuffer;
t.parents = _T("parents");
t.parents += strbuffer;
t.brithday.year = t.brithday.month = t.brithday.day = i;
::CoCreateGuid(&guid);
if (i == findata.front()) {
guid = findata.front().findguid;
findata.pop();
}
CString temp(GUIDToWstring(&guid).c_str());
test_data.insert(hashmap_d_type::value_type(temp, t));
}
return test_data.size();
}
void printHashMap(stru_find findata, hashmap_d_type& test_data) {
CString temp;
int n = findata.size();
for (int i = 0; i < n; i++)
{
temp.Empty();
temp.Append(GUIDToWstring(&findata.front().findguid).c_str());
hashmap_d_type::iterator ifind = test_data.find(temp);
if (ifind != test_data.end()) {
wcout << endl << L"left find count: " << findata.size() << " find index:"<< findata.front() << endl
<< "data key :" << temp.GetString() << endl
<< L"name :" << (*ifind).second.name.GetString()<< endl
<< L"address :"<< (*ifind).second.adress.GetString() <<endl
<< L"parents :"<<(*ifind).second.parents.GetString() << endl
<< L"birthday :" << (*ifind).second.brithday.year << " "<< (*ifind).second.brithday.month<<" "<<(*ifind).second.brithday.day << endl;
findata.pop();
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
size_t loadsize = 0;
string loadtype;
stru_find finddata;
map_d_type test_mapdata;
cmap_d_type test_cmapdata;
hashmap_d_type test_hashmapdata;
unordemap_d_type test_unordermapdata;
createfinddata(finddata);
time_t start = clock();
//loadsize = loadMapData(finddata, test_mapdata);loadtype.empty();loadtype.append("In map type:");
//loadsize = loadCmapData(finddata, test_cmapdata);loadtype.empty();loadtype.append("In cmap type:");
//loadsize = loadHashMapData(finddata, test_hashmapdata); loadtype.empty();loadtype.append("In hash_map type:");
loadsize = loadUnoderMapData(finddata, test_unordermapdata); loadtype.empty();loadtype.append("In unordered_map type:");
time_t end = clock();
cout << loadtype << endl << "load "<< loadsize <<" data useing time:" << end - start << "/ms" << endl;
start = clock();
//printfmap(finddata, test_mapdata);
//printfcmap(finddata, test_cmapdata);
//printHashMap(finddata, test_hashmapdata);
printUnoderMap(finddata, test_unordermapdata);
end = clock();
test_mapdata.clear();
test_cmapdata.RemoveAll();
cout << endl << "find "<<finddata.size()<<" data using time:" << end - start << "/ms" << endl;
system("pause");
return 0;
}
// //