-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
116 lines (95 loc) · 2.94 KB
/
main.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
#include <cassert>
#include <iostream>
#include <string>
#include <time.h>
#include <unordered_map>
#include "hash_table.hpp"
void profile_rehash(int n)
{
clock_t start, end;
double cpu_time_used;
HashTable<int, int> table;
std::unordered_map<int, int> uTable;
for (int i = 0; i < n; i++) {
start = clock();
uTable.insert({ i, i * 5 });
end = clock();
cpu_time_used = end - start;
std::cout << "unordered_map i: " << i << " time: " << cpu_time_used
<< " size: " << uTable.bucket_count() << std::endl;
}
for (int i = 0; i < n; i++) {
start = clock();
table.Insert(i, i * 5);
end = clock();
cpu_time_used = end - start;
std::cout << "hash_table i: " << i << " time: " << cpu_time_used
<< " size: " << table.BucketCount() << std::endl;
}
}
int main(int argc, char** argv)
{
clock_t start, end;
double cpu_time_used;
HashTable<int, int> table;
std::unordered_map<int, int> uTable;
int j = 0;
if (argc > 1) {
j = atoi(argv[1]);
if (argc > 2) {
profile_rehash(j);
return 0;
}
}
uTable.reserve(static_cast<double>(j / uTable.max_load_factor()) + 1);
table.Reserve(static_cast<double>(j / table.MaxLoadFactor()) + 1);
start = clock();
for (int i = 0; i < j; i++) {
uTable.insert({ i, i * 5 });
}
end = clock();
cpu_time_used = end - start;
std::cout << "unordered_map insert() cpu time for " << j << " elements: " << cpu_time_used
<< " us" << std::endl;
start = clock();
for (int i = 0; i < j; i++) {
table.Insert(i, i * 5);
}
end = clock();
cpu_time_used = end - start;
std::cout << "HashTable insert() cpu time for " << j << " elements: " << cpu_time_used << " us"
<< std::endl;
start = clock();
for (int i = 0; i < j; i++) {
auto x = uTable.at(i);
}
end = clock();
cpu_time_used = end - start;
std::cout << "unordered_map at() cpu time for " << j << " elements: " << cpu_time_used << " us"
<< std::endl;
start = clock();
for (int i = 0; i < j; i++) {
auto x = table.At(i);
}
end = clock();
cpu_time_used = end - start;
std::cout << "HashTable at() cpu time for " << j << " elements: " << cpu_time_used << " us"
<< std::endl;
start = clock();
for (int i = 0; i < j; i++) {
uTable.erase(i);
}
end = clock();
cpu_time_used = end - start;
std::cout << "unordered_map erase() cpu time for " << j << " elements: " << cpu_time_used
<< " us" << std::endl;
start = clock();
for (int i = 0; i < j; i++) {
table.Remove(i);
}
end = clock();
cpu_time_used = end - start;
std::cout << "HashTable erase() cpu time for " << j << " elements: " << cpu_time_used << " us"
<< std::endl;
return 0;
}