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
|
#include "linear-probing.hpp"
#include "separate-chaining.hpp"
#include <chrono>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <map>
std::vector<std::string> get_words(const std::string& file)
{
std::vector<std::string> words;
std::ifstream ifs("harry.txt");
if (!ifs.is_open())
{
std::cout << "File retrieval failed" << std::endl;
return {};
}
while (!ifs.eof())
{
std::string word;
ifs >> word;
words.emplace_back(word);
}
return words;
}
void test_hashmap_separate_chaining(size_t initial_capacity, double lf)
{
std::cout << "[Separate chaining tests]: Running test for " << initial_capacity << " initial capacity and " << lf
<< " load factor." << std::endl;
separate_chaining_hashmap<std::string, int> mp;
std::vector<std::string> words = get_words("harry.txt");
std::cout << "[Separate chaining tests]: Parsed " << words.size() << " words in std::vector." << std::endl;
unsigned dict_size = 0;
auto begin = std::chrono::high_resolution_clock::now();
for (const auto& word : words)
{
try
{
++mp.modify(word);
}
catch (...)
{
++dict_size;
mp.insert(word, 1);
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
std::cout << "[Separate chaining tests]: Parsed " << words.size() << " words in separate_chaining_hashmap<std::string, int>."
<< std::endl;
std::cout << "[Separate chaining tests]: Dict size: " << dict_size << std::endl;
std::cout << "[Separate chaining tests]: Buckets count: " << mp.bucket_count() << std::endl;
std::cout << "[Separate chaining tests]: Size: " << mp.size() << std::endl;
std::cout << "[Separate chaining tests]: Time: " << duration << " ms." << std::endl;
}
void test_hashmap_linear_probing(size_t jump_value, size_t initial_capacity, double lf)
{
std::cout << "[Linear probing tests]: Running test for " << initial_capacity << " initial capacity and " << lf
<< " load factor and " << jump_value << " as jump value." << std::endl;
linear_probing_hashmap<std::string, int> mp(initial_capacity, lf, jump_value);
std::vector<std::string> words = get_words("harry.txt");
std::cout << "[Linear probing tests]: Parsed " << words.size() << " words in std::vector." << std::endl;
int dict_size = 0;
auto begin = std::chrono::high_resolution_clock::now();
for (const auto& word : words)
{
try
{
++mp.modify(word);
}
catch (...)
{
++dict_size;
mp.insert(word, 1);
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
std::cout << "[Linear probing tests]: Parsed " << words.size() << " words in linear_probing_hashmap<std::string, int>."
<< std::endl;
std::cout << "[Linear probing tests]: Dict size: " << dict_size << std::endl;
std::cout << "[Linear probing tests]: Buckets count: " << mp.bucket_count() << std::endl;
std::cout << "[Linear probing tests]: Size: " << mp.size() << std::endl;
std::cout << "[Linear probing tests]: Time: " << duration << " ms." << std::endl;
}
void test_hashmap_std()
{
std::unordered_map<std::string, int> mp;
std::vector<std::string> words = get_words("harry.txt");
std::cout << "[std::unordered_map tests]: Parsed " << words.size() << " words in std::vector." << std::endl;
auto begin = std::chrono::high_resolution_clock::now();
for (const auto& word : words)
{
++mp[word];
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
std::cout << "[std::unordered_map tests]: Parsed " << words.size() << " words in std::unordered_map<std::string, int>."
<< std::endl;
std::cout << "[std::unordered_map tests]: Buckets count: " << mp.bucket_count() << std::endl;
std::cout << "[std::unordered_map tests]: Size: " << mp.size() << std::endl;
std::cout << "[std::unordered_map tests]: Time: " << duration << " ms." << std::endl;
}
void test_map_std()
{
std::map<std::string, int> mp;
std::vector<std::string> words = get_words("harry.txt");
std::cout << "[std::map tests]: Parsed " << words.size() << " words in std::vector." << std::endl;
auto begin = std::chrono::high_resolution_clock::now();
for (const auto& word : words)
{
++mp[word];
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
std::cout << "[std::map tests]: Parsed " << words.size() << " words in std::map<std::string, int>."
<< std::endl;
std::cout << "[std::map tests]: Size: " << mp.size() << std::endl;
std::cout << "[std::map tests]: Time: " << duration << " ms." << std::endl;
}
int main()
{
test_hashmap_separate_chaining(1000, 0.7);
std::cout << std::endl;
test_hashmap_linear_probing(1, 997, 0.7);
std::cout << std::endl;
test_hashmap_std();
std::cout << std::endl;
test_map_std();
}
|