-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
332 lines (283 loc) · 12.1 KB
/
main.cc
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "binary_search.cc"
#include "hash_table.cc"
#include "bloom.cc"
#include "cuckoo.cc"
//#include "trie.cc"
using namespace std;
// void trie(string dictFile, string queriesFile, int nRep) {
// double _elements = 0;
// double timeInsert = 0;
// double timeFind = 0;
// double timeTotal = 0;
// double numFoundElements = 0;
// double numNotFoundElements = 0;
// for (int i = 0; i < nRep; ++i) {
// fstream dict(dictFile, ios_base::in);
// fstream queries(queriesFile, ios_base::in);
// Trie t;
// unsigned int a;
// while (dict >> a) {
// t.insert(a);
// }
// while (queries >> a) {
// t.find(a);
// }
// timeInsert += t.timeInsert;
// timeFind += t.timeFind;
// timeTotal += t.timeTotal;
// numFoundElements += t.numFoundElements;
// numNotFoundElements += t.numNotFoundElements;
// _elements += t._elements;
// }
// timeTotal /= nRep;
// timeInsert /= nRep;
// timeFind /= nRep;
// numFoundElements /= nRep;
// numNotFoundElements /= nRep;
// _elements /= nRep;
// cout << endl << endl << " ---- Trie Results ----" << endl << endl;
// cout << "find(k): average search time:\t" << double(timeTotal)/(numFoundElements+numNotFoundElements) << endl;
// cout << "find(k): total search time:\t" << timeFind << endl;
// cout << "find(k): number of successful queries:\t" << numFoundElements << endl;
// cout << "find(k): number of unsuccessful queries:\t" << numNotFoundElements << endl;
// cout << "insert(k): average insertion time:\t" << double(timeTotal)/(_elements) << endl;
// cout << "insert(k): total insertion time:\t" << timeInsert << endl;
// cout << "insert(k): number of elements:\t" << _elements << endl;
// }
void cuckoo(string dictFile, string queriesFile, int nRep, int m) {
double timeTotal = 0;
double numFoundElements = 0;
double numNotFoundElements = 0;
double _elements = 0;
double timeFind = 0;
double timeInsert = 0;
double numRehash = 0;
double rehashTime = 0;
double numCallsHashFunction1 = 0;
double numCallsHashFunction2 = 0;
double numBounces = 0;
for (int i = 0; i < nRep; ++i) {
fstream dict(dictFile, ios_base::in);
fstream queries(queriesFile, ios_base::in);
CuckooHash c(m, DIVISION_KNUTH, MULT_METHOD);
unsigned int a;
while (dict >> a) {
c.insert(a);
}
while (queries >> a) {
c.find(a);
}
timeTotal += c.timeTotal;
timeFind += c.timeFind;
numFoundElements += c.numFoundElements;
numNotFoundElements += c.numNotFoundElements;
_elements += c._elements;
timeInsert += c.timeInsert;
numRehash += c.numRehash;
rehashTime += c.rehashTime;
numCallsHashFunction1 += c.numCallsHashFunction1;
numCallsHashFunction2 += c.numCallsHashFunction2;
numBounces += c.numBounces;
}
timeTotal /= nRep;
timeFind /= nRep;
numFoundElements /= nRep;
numNotFoundElements /= nRep;
_elements /= nRep;
timeInsert /= nRep;
numRehash /= nRep;
rehashTime /= nRep;
numCallsHashFunction1 /= nRep;
numCallsHashFunction2 /= nRep;
numBounces /= nRep;
cout << endl << endl << " ---- Cuckoo Results ----" << endl << endl;
cout << "find(k): average search time:\t" << double(timeFind)/(numFoundElements+numNotFoundElements) << endl;
cout << "find(k): total search time:\t" << timeFind << endl;
cout << "find(k): number of successful queries:\t" << numFoundElements << endl;
cout << "find(k): number of unsuccessful queries:\t" << numNotFoundElements << endl;
cout << "insert(k): average insertion time:\t" << double(timeInsert)/(_elements) << endl;
cout << "insert(k): total insertion time:\t" << timeInsert << endl;
cout << "insert(k): total number of bounces:\t" << numBounces << endl;
cout << "insert(k): number of elements:\t" << _elements << endl;
cout << "rehash(): number of rehashes:\t" << numRehash << endl;
cout << "rehash(): average time of each rehash:\t" << double(rehashTime)/(numRehash) << endl;
cout << "rehash(): average time between each rehash:\t" << double(timeTotal-rehashTime)/(numRehash) << endl;
cout << " : number of calls to hash1:\t" << numCallsHashFunction1 << endl;
cout << " : number of calls to hash2:\t" << numCallsHashFunction2 << endl;
}
void hashTable(string dictFile, string queriesFile, int nRep, int m, double maxLoadFactor) {
double timeTotal = 0;
double numFoundElements = 0;
double numNotFoundElements = 0;
double _elements = 0;
double timeFind = 0;
double timeInsert = 0;
double numRehash = 0;
double rehashTime = 0;
double numCallsHashFunction = 0;
for (int i = 0; i < nRep; ++i) {
fstream dict(dictFile, ios_base::in);
fstream queries(queriesFile, ios_base::in);
HashTable ht(m, maxLoadFactor, DIVISION_METHOD);
unsigned int a;
while (dict >> a) {
ht.insert(a);
}
while (queries >> a) {
ht.find(a);
}
timeTotal += ht.timeTotal;
timeFind += ht.timeFind;
numFoundElements += ht.numFoundElements;
numNotFoundElements += ht.numNotFoundElements;
_elements += ht._elements;
timeInsert += ht.timeInsert;
numRehash += ht.numRehash;
rehashTime += ht.rehashTime;
numCallsHashFunction += ht.numCallsHashFunction;
}
timeTotal /= nRep;
timeFind /= nRep;
numFoundElements /= nRep;
numNotFoundElements /= nRep;
_elements /= nRep;
timeInsert /= nRep;
numRehash /= nRep;
rehashTime /= nRep;
numCallsHashFunction /= nRep;
cout << endl << endl << " ---- Hash Table Results ----" << endl << endl;
cout << "find(k): average search time:\t" << double(timeTotal)/(numFoundElements+numNotFoundElements) << endl;
cout << "find(k): total search time:\t" << timeFind << endl;
cout << "find(k): number of successful queries:\t" << numFoundElements << endl;
cout << "find(k): number of unsuccessful queries:\t" << numNotFoundElements << endl;
cout << "insert(k): average insertion time:\t" << double(timeTotal)/(_elements) << endl;
cout << "insert(k): total insertion time:\t" << timeInsert << endl;
cout << "insert(k): number of elements:\t" << _elements << endl;
cout << "rehash(): number of rehashes:\t" << numRehash << endl;
cout << "rehash(): average time of each rehash:\t" << double(rehashTime)/(numRehash) << endl;
cout << "rehash(): average time between each rehash:\t" << double(timeTotal-rehashTime)/(numRehash) << endl;
cout << " : number of calls to hash:\t" << numCallsHashFunction << endl;
}
void bloomFilter(string dictFile, string queriesFile, int nRep, int m) {
double numCallsH1H2 = 0;
double numFoundElements = 0;
double numNotFoundElements = 0;
double numAlmostFoundElements = 0;
double timeFind = 0;
double timeInsert = 0;
double nElements = 0;
for (int i = 0; i < nRep; ++i) {
fstream dict(dictFile, ios_base::in);
fstream queries(queriesFile, ios_base::in);
BloomFilter bf(DIVISION_KNUTH, MULT_METHOD, m);
vector<unsigned int> v;
unsigned int a;
while (dict >> a) bf.insert(a);
while (queries >> a) bf.find(a);
numCallsH1H2 += bf.numCallsH1H2;
numFoundElements += bf.numFoundElements;
numNotFoundElements += bf.numNotFoundElements;
numAlmostFoundElements += bf.numAlmostFoundElements;
timeFind += bf.timeFind;
timeInsert += bf.timeInsert;
nElements += bf.nElements;
}
numCallsH1H2 /= (double) nRep;
numFoundElements /= (double) nRep;
numNotFoundElements /= (double) nRep;
numAlmostFoundElements /= (double) nRep;
timeFind /= (double) nRep;
timeInsert /= double(nRep);
nElements /= double(nRep);
cout << endl << endl << " ---- Bloom Filter Results ----" << endl << endl;
cout << "find(k): average search time:\t" << double(timeFind)/(numFoundElements+numNotFoundElements) << endl;
cout << "find(k): total search time:\t" << timeFind << endl;
cout << "find(k): number of successful queries:\t" << numFoundElements << endl;
cout << "find(k): number of unsuccessful queries:\t" << numNotFoundElements << endl;
cout << "insert(k): average insertion time:\t" << double(timeInsert)/(nElements) << endl;
cout << "insert(k): total insertion time:\t" << timeInsert << endl;
cout << "insert(k): number of elements:\t" << nElements << endl;
cout << "hash funcs: number of calls to hash functions:\t" << numCallsH1H2 << endl;
}
void binarySearch(string dictFile, string queriesFile, int nRep) {
double numFoundElements = 0;
double numNotFoundElements = 0;
double numSortComparisons = 0;
double numKeyCompFoundElements = 0;
double numKeyCompNotFoundElements = 0;
double timeTotal = 0;
for (int i = 0; i < nRep; ++i) {
fstream dict(dictFile, ios_base::in);
fstream queries(queriesFile, ios_base::in);
BinarySearch bs;
vector<unsigned int> v;
unsigned int a;
while (dict >> a) v.push_back(a);
bs.setDictionary(v);
while (queries >> a) bs.find(a);
numFoundElements += bs.numFoundElements;
numNotFoundElements += bs.numNotFoundElements;
numSortComparisons += bs.numSortComparisons;
numKeyCompFoundElements += bs.numKeyCompFoundElements;
numKeyCompNotFoundElements += bs.numKeyCompNotFoundElements;
timeTotal += bs.timeTotal;
}
numFoundElements /= (double) nRep;
numNotFoundElements /= (double) nRep;
numSortComparisons /= (double) nRep;
numKeyCompFoundElements /= (double) nRep;
numKeyCompNotFoundElements /= (double) nRep;
timeTotal /= double(nRep);
cout << endl << "---- Binary Search Results ----" << endl;
cout << "find(k): comparisons between keys:\t" << double(numKeyCompFoundElements+numKeyCompNotFoundElements)/(numFoundElements+numNotFoundElements) << endl;
cout << "find(k): average search time:\t" << double(timeTotal)/(numFoundElements+numNotFoundElements) << endl;
cout << "find(k): total search time:\t" << timeTotal << endl;
cout << "find(k): number of successful queries:\t" << numFoundElements << endl;
cout << "find(k): number of unsuccessful queries:\t" << numNotFoundElements << endl;
cout << "setDictionary(v): sort comparisons:\t" << numSortComparisons << endl;
cout << endl;
cout << " ** For found elements **" << endl;
cout << "find(k): average comparisons between keys:\t" << (double) numKeyCompFoundElements/numFoundElements << endl;
cout << endl;
cout << " ** For not found elements **" << endl;
cout << "find(k): average comparisons between keys:\t" << (double) numKeyCompNotFoundElements/numNotFoundElements << endl;
}
void usage(string name) {
cout << endl << "Usage: " << name << " dict_file queries_file n_rep hashtable_size bloomfilter_size max_loadFactor cuckoo_size" << endl << endl;
cout << " - dict_file: fitxer amb el conjunt de paraules a inserir a cada conjunt" << endl;
cout << " - queries_file: fitxer amb el conjunt de paraules a cercar al conjunt" << endl;
cout << " - n_rep: nombre de vegades que es repeteix cada execucio (com a resultats s'imprimeix la mitjana de les n_rep execucions)" << endl;
cout << " - hashtable_size: mida de la taula de hash (nombre de caselles)" << endl;
cout << " - bloomfilter_size: mida del filtre de bloom (nombre de caselles)" << endl;
cout << " - max_loadFactor: factor de carrega a partir del qual es fara rehash de la taula de hash" << endl;
cout << " - cuckoo_size: size of each table of cuckoo hashing structure" << endl;
exit(-1);
}
int main(int argc, char* argv[]) {
if (argc < 8) usage(argv[0]);
string dictFile = argv[1];
string queriesFile = argv[2];
int nRep = atoi(argv[3]);
int hashTableSize = atoi(argv[4]);
int bloomFilterSize = atoi(argv[5]);
double maxLoadFactor= double(atof(argv[6]));
int cuckooSize = atoi(argv[7]);
cout << "Diccionari:\t" + dictFile << endl;
cout << "Queries:\t" + queriesFile << endl;
cerr << "Diccionari:\t" + dictFile << endl;
cerr << "Queries:\t" + queriesFile << endl;
cerr << endl << "binary search ..." << endl;
binarySearch(dictFile, queriesFile, nRep);
cerr << endl << "filtre de bloom ..." << endl;
bloomFilter(dictFile, queriesFile, nRep, bloomFilterSize);
cerr << endl << "taula de hash ..." << endl;
hashTable(dictFile, queriesFile, nRep, hashTableSize, maxLoadFactor);
cerr << endl << "cuckoo ..." << endl;
cuckoo(dictFile, queriesFile, nRep, hashTableSize);
//cerr << endl << "trie ..." << endl;
//trie(dictFile, queriesFile, nRep);
cout << endl << " --- FI EXPERIMENT --- " << endl << endl;
}