-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchdict.cpp
More file actions
241 lines (204 loc) · 7.49 KB
/
Copy pathbenchdict.cpp
File metadata and controls
241 lines (204 loc) · 7.49 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
240
/**
* Name: Rui Deng
* Dadong Jing
* Data: Feb 18, 2016
* Overview: implement a program that times our implementation of find
* methods in our dictionary classes
* Assignment number: PA3
*/
#include "util.hpp"
#include "DictionaryTrie.hpp"
#include "DictionaryBST.hpp"
#include "DictionaryHashtable.hpp"
#include <fstream>
#include <sstream>
/**
* time test on find method in trie
*/
void testFindTrie(unsigned int min_size,unsigned int min_step_size,
int numofIteration, std::string dict_filename)
{
//initalize the input stream
std::ifstream in;
//indicate the dictionary we are testing
std::cout<<"DictionaryTrie"<<std::endl;
//initialize timer and variables to store time
Timer T;
long long time_duration = 0;
long long total_time = 0;
long long average_time = 0;
//for loop go through number of iteration times
for(int i = 0; i < numofIteration; i++)
{
//open stream, create new trie and load
total_time = 0;
in.open(dict_filename,std::ios::binary);
DictionaryTrie* dictionary_trie = new DictionaryTrie();
Utils::load_dict(*dictionary_trie,in,min_size+i*min_step_size);
//give warning if reach the end but still do the finding
if(in.eof())
{
std::cout<<"Warning: words is not enough in file"<<std::endl;
}
//while loop finding 10 words 1000 times
int j = 0;
while( j < 1000)
{
T.begin_timer();
dictionary_trie->find("fweijgowjgieojgi");
dictionary_trie->find("pgiwjegiwobniioa");
dictionary_trie->find("agiwnbionieowigj");
dictionary_trie->find("fwjionbnwianbwoa");
dictionary_trie->find("wobiwnbwonblddvi");
dictionary_trie->find("cngiwbonabnlnvva");
dictionary_trie->find("idnaboakmacnrbni");
dictionary_trie->find("ljbiaonbnrnoenbr");
dictionary_trie->find("wbirobncknvbonbr");
dictionary_trie->find("ibonanblkxbnaeon");
time_duration = T.end_timer();
total_time = total_time + time_duration;
j++;
}
//get the average time and print out
average_time = total_time/1000;
std::cout<<min_size+i*min_step_size<<"\t"<<average_time<<std::endl;
//clean up
delete dictionary_trie;
in.close();
}
std::cout<<""<<std::endl;
}
/**
* time test on find method in BST
*/
void testFindBST(unsigned int min_size,unsigned int min_step_size,
int numofIteration, std::string dict_filename)
{
//initialize the input stream
std::ifstream in;
//indicate the dictionary we are testing
std::cout<<"DictionaryBST"<<std::endl;
//initialize timer and variables to store time
Timer T;
long long time_duration = 0;
long long total_time = 0;
long long average_time = 0;
//for loop go through number of iteration times
for(int i = 0; i < numofIteration; i++)
{
//open stream, create new BST and load
total_time = 0;
in.open(dict_filename,std::ios::binary);
DictionaryBST* dictionary_bst = new DictionaryBST();
Utils::load_dict(*dictionary_bst,in,min_size+i*min_step_size);
//give warning if reach the end but still do the finding
if(in.eof())
{
std::cout<<"Warning: words is not enough in file"<<std::endl;
}
//while loop finding 10 words 1000 times
int j = 0;
while( j < 1000)
{
T.begin_timer();
dictionary_bst->find("fweijgowjgieojgi");
dictionary_bst->find("pgiwjegiwobniioa");
dictionary_bst->find("agiwnbionieowigj");
dictionary_bst->find("fwjionbnwianbwoa");
dictionary_bst->find("wobiwnbwonblddvi");
dictionary_bst->find("cngiwbonabnlnvva");
dictionary_bst->find("idnaboakmacnrbni");
dictionary_bst->find("ljbiaonbnrnoenbr");
dictionary_bst->find("wbirobncknvbonbr");
dictionary_bst->find("ibonanblkxbnaeon");
time_duration = T.end_timer();
total_time = total_time + time_duration;
j++;
}
//get the average time and print out
average_time = total_time/1000;
std::cout<<min_size+i*min_step_size<<"\t"<<average_time<<std::endl;
//clean up
delete dictionary_bst;
in.close();
}
std::cout<<""<<std::endl;
}
/**
* time test on find method in HashTable
*/
void testFindHashtable(unsigned int min_size,unsigned int min_step_size,
int numofIteration, std::string dict_filename)
{
//initialize the input stream
std::ifstream in;
//indicate the dictionary we are testing
std::cout<<"DictionaryHashtable"<<std::endl;
//initialize timer and variables to store time
Timer T;
long long time_duration = 0;
long long total_time = 0;
long long average_time = 0;
//for loop go through number of iteration times
for(int i = 0; i < numofIteration; i++)
{
//open stream, create new HashTable and load
total_time = 0;
in.open(dict_filename,std::ios::binary);
DictionaryHashtable* dictionary_hashtable = new DictionaryHashtable();
Utils::load_dict(*dictionary_hashtable,in,min_size+i*min_step_size);
//give warning if reach the end but still do the finding
if(in.eof())
{
std::cout<<"Warning: words is not enough in file"<<std::endl;
}
//while loop finding 10 words 1000 times
int j = 0;
while( j < 1000)
{
T.begin_timer();
dictionary_hashtable->find("fweijgowjgieojgi");
dictionary_hashtable->find("pgiwjegiwobniioa");
dictionary_hashtable->find("agiwnbionieowigj");
dictionary_hashtable->find("fwjionbnwianbwoa");
dictionary_hashtable->find("wobiwnbwonblddvi");
dictionary_hashtable->find("cngiwbonabnlnvva");
dictionary_hashtable->find("idnaboakmacnrbni");
dictionary_hashtable->find("ljbiaonbnrnoenbr");
dictionary_hashtable->find("wbirobncknvbonbr");
dictionary_hashtable->find("ibonanblkxbnaeon");
time_duration = T.end_timer();
total_time = total_time + time_duration;
j++;
}
//get the average time and print out
average_time = total_time/1000;
std::cout<<min_size+i*min_step_size<<"\t"<<average_time<<std::endl;
//clean up
delete dictionary_hashtable;
in.close();
}
}
/**
* main method to run the whole program, calling three methods above
*/
int main(int argc, char* argv[])
{
//check number of arguments and prompt user if wrong
if(argc < 5)
{
std::cout << "Incorrect number of arguments." << std::endl;
std::cout << "\t First argument: name of dictionary file." << std::endl;
std::cout << std::endl;
exit(-1);
}
//get min_size, min_step_size and number of iterations from arguments
unsigned int min_size = (unsigned int) std::stoi(argv[1]);
unsigned int min_step_size = (unsigned int) std::stoi(argv[2]);
int numofIteration = std::stoi(argv[3]);
//call all three test methods
testFindTrie(min_size,min_step_size,numofIteration,argv[4]);
testFindBST(min_size,min_step_size,numofIteration,argv[4]);
testFindHashtable(min_size,min_step_size,numofIteration,argv[4]);
return 0;
}