-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_performance_HashMap.cpp
More file actions
239 lines (189 loc) · 9.63 KB
/
Copy pathtest_performance_HashMap.cpp
File metadata and controls
239 lines (189 loc) · 9.63 KB
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
#include "catch.hpp"
#include "HashMap.hpp" // 包含您的 HashMap 实现
#include <unordered_map>
#include <chrono>
#include <iostream>
#include<string>
#include <random>
TEST_CASE("Performance Tests", "[performance]") {
const int NUM_ELEMENTS = 1000000; // 测试元素数量
// 准备测试数据
std::vector<std::pair<int, int>> testData;
for (int i = 0; i < NUM_ELEMENTS; ++i) {
testData.push_back({ i, i });
}
HashMap<int, int> hashMap;
std::unordered_map<int, int> unorderedMap;
// Insertion for HashMap
auto start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
hashMap.Insert(kv);
}
auto end = std::chrono::high_resolution_clock::now();
auto hashMapInsertionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "HashMap insertion time: " << hashMapInsertionDuration << " ms\n";
// Insertion for unordered_map
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
unorderedMap.insert(kv);
}
end = std::chrono::high_resolution_clock::now();
auto unorderedMapInsertionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "std::unordered_map insertion time: " << unorderedMapInsertionDuration << " ms\n";
//REQUIRE(hashMapInsertionDuration < unorderedMapInsertionDuration * 2); // 假设性能差距不超过2倍
//}
//SECTION("Lookup Test") {
// Lookup for HashMap
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
hashMap.getValue(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto hashMapLookupDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "HashMap lookup time: " << hashMapLookupDuration << " ms\n";
// Lookup for unordered_map
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
unorderedMap.find(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto unorderedMapLookupDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "std::unordered_map lookup time: " << unorderedMapLookupDuration << " ms\n";
//REQUIRE(hashMapLookupDuration < unorderedMapLookupDuration * 2); // 假设性能差距不超过2倍
// }
// Deletion for HashMap
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
hashMap.Remove(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto hashMapDeletionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "HashMap deletion time: " << hashMapDeletionDuration << " ms\n";
// Deletion for unordered_map
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
unorderedMap.erase(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto unorderedMapDeletionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "std::unordered_map deletion time: " << unorderedMapDeletionDuration << " ms\n";
//REQUIRE(hashMapDeletionDuration < unorderedMapDeletionDuration * 2); // 假设性能差距不超过2倍
}
TEST_CASE("Performance Tests with int and std::string keys", "[performance]") {
const int NUM_ELEMENTS = 1000000; // 测试元素数量减少,因为字符串处理通常更慢
// 准备整数测试数据
std::vector<std::pair<int, int>> testDataInt;
for (int i = 0; i < NUM_ELEMENTS; ++i) {
testDataInt.push_back({ i, i });
}
// 准备字符串测试数据
std::vector<std::pair<std::string, int>> testDataString;
for (int i = 0; i < NUM_ELEMENTS; ++i) {
testDataString.push_back({ "Key" + std::to_string(i), i });
}
HashMap<std::string, int> hashMapString;
std::unordered_map<std::string, int> unorderedMapString;
// 插入测试
// Insertion for HashMap with string keys
auto start = std::chrono::high_resolution_clock::now();
for (auto& kv : testDataString) {
hashMapString.Insert(kv);
}
auto end = std::chrono::high_resolution_clock::now();
auto hashMapInsertionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "HashMap (string keys) insertion time: " << hashMapInsertionDuration << " ms\n";
// Insertion for unordered_map with string keys
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testDataString) {
unorderedMapString.insert(kv);
}
end = std::chrono::high_resolution_clock::now();
auto unorderedMapInsertionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "std::unordered_map (string keys) insertion time: " << unorderedMapInsertionDuration << " ms\n";
// 查找测试
// 类似地,对字符串键进行查找测试
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testDataString) {
hashMapString.getValue(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto hashMapLookupDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "HashMap (string keys) find time: " << hashMapLookupDuration << " ms\n";
// Insertion for unordered_map with string keys
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testDataString) {
unorderedMapString.find(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto unorderedMapLookupDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "std::unordered_map (string keys) find time: " << unorderedMapLookupDuration << " ms\n";
// 删除测试
}
TEST_CASE("Random Performance Tests", "[performance]") {
const int NUM_ELEMENTS = 1000000; // 测试元素数量
// 准备测试数据
std::vector<std::pair<int, int>> testData;
std::random_device rd; // Random device
std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> distrib(1, 1000000); // Range of random numbers
for (int i = 0; i < NUM_ELEMENTS; ++i) {
int randomKey = distrib(gen); // Generate a random integer
int randomValue = distrib(gen); // Generate another random integer
testData.push_back({ randomKey, randomValue });
}
HashMap<int, int> hashMap;
std::unordered_map<int, int> unorderedMap;
// Insertion for HashMap
auto start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
hashMap.Insert(kv);
}
auto end = std::chrono::high_resolution_clock::now();
auto hashMapInsertionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "HashMap insertion time: " << hashMapInsertionDuration << " ms\n";
// Insertion for unordered_map
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
unorderedMap.insert(kv);
}
end = std::chrono::high_resolution_clock::now();
auto unorderedMapInsertionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "std::unordered_map insertion time: " << unorderedMapInsertionDuration << " ms\n";
//REQUIRE(hashMapInsertionDuration < unorderedMapInsertionDuration * 2); // 假设性能差距不超过2倍
//}
//SECTION("Lookup Test") {
// Lookup for HashMap
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
hashMap.getValue(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto hashMapLookupDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "HashMap lookup time: " << hashMapLookupDuration << " ms\n";
// Lookup for unordered_map
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
unorderedMap.find(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto unorderedMapLookupDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "std::unordered_map lookup time: " << unorderedMapLookupDuration << " ms\n";
//REQUIRE(hashMapLookupDuration < unorderedMapLookupDuration * 2); // 假设性能差距不超过2倍
// }
// Deletion for HashMap
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
hashMap.Remove(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto hashMapDeletionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "HashMap deletion time: " << hashMapDeletionDuration << " ms\n";
// Deletion for unordered_map
start = std::chrono::high_resolution_clock::now();
for (auto& kv : testData) {
unorderedMap.erase(kv.first);
}
end = std::chrono::high_resolution_clock::now();
auto unorderedMapDeletionDuration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "std::unordered_map deletion time: " << unorderedMapDeletionDuration << " ms\n";
}