-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer_main.cpp
383 lines (335 loc) · 12.9 KB
/
timer_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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* timer_main.cpp
*
* CS 15 homework 2
* by Milod Kazerounian, July 2022
*
* A driver program for timing various CharLinkedList operations.
* Once you have completed your CharLinkedList implementation,
* from terminal run "make timer" to compile an executable
* from this driver. Run the executable to view a table of times.
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <utility>
#include <chrono>
#include "CharLinkedList.h"
using namespace std;
using namespace std::chrono;
/**************************
* TABLE FORMAT CONSTANTS *
**************************/
const int TABLE_WIDTH = 70;
const int OP_ENTRY_WIDTH = 0.75 * TABLE_WIDTH;
const int TIME_ENTRY_WIDTH = TABLE_WIDTH - OP_ENTRY_WIDTH;
const char HORIZONTAL_SEP = '-';
const char FILL = ' ';
const string LINE = string(TABLE_WIDTH, HORIZONTAL_SEP);
/**************************
* LIST TESTING CONSTANTS *
**************************/
const int DEFAULT_LIST_SIZE = 10000; // Reduced from 100,000 to avoid stack overflow seg faults
const int DEFAULT_NUM_OPS = 100;
const char DUMMY_CHAR = 'a';
/**************************
* FUNCTION DECLARATIONS *
**************************/
template<typename T>
void printEntry(const string action, const T time);
CharLinkedList *generateRandList(const int size);
pair<string, int> pushAtFront_test();
pair<string, int> pushAtBack_test();
pair<string, int> insertAtMiddle_test();
pair<string, int> popFromFront_test();
pair<string, int> popFromBack_test();
pair<string, int> removeAtMiddle_test();
pair<string, int> first_test();
pair<string, int> last_test();
pair<string, int> elementAtMiddle_test();
/*
* name: main
* purpose: runs timing tests and prints table
* returns: exit code of 0 when run to completion
*/
int main()
{
srand(time(0));
/***** INSERTION TESTS *****/
pair<string, int> pushFront = pushAtFront_test();
pair<string, int> pushBack = pushAtBack_test();
pair<string, int> insertMiddle = insertAtMiddle_test();
/***** REMOVAL TESTS *****/
pair<string, int> popFront = popFromFront_test();
pair<string, int> popBack = popFromBack_test();
pair<string, int> removeMiddle = removeAtMiddle_test();
/***** ACCESS TESTS *****/
pair<string, int> first = first_test();
pair<string, int> last = last_test();
pair<string, int> middle = elementAtMiddle_test();
cout << "\nAll operations below are run on a separate, randomly generated\n"
<< "CharLinkedList of size " << DEFAULT_LIST_SIZE <<".\n\n";
/***** PRINT TABLE *****/
cout << LINE << endl;
printEntry("INSERTION OPERATION", "Time (nanoseconds)");
cout << LINE << endl;
printEntry(pushFront.first, pushFront.second);
cout << LINE << endl;
printEntry(pushBack.first, pushBack.second);
cout << LINE << endl;
printEntry(insertMiddle.first, insertMiddle.second);
cout << LINE << endl;
printEntry("REMOVAL OPERATION", "Time (nanoseconds)");
cout << LINE << endl;
printEntry(popFront.first, popFront.second);
cout << LINE << endl;
printEntry(popBack.first, popBack.second);
cout << LINE << endl;
printEntry(removeMiddle.first, removeMiddle.second);
cout << LINE << endl;
printEntry("ACCESS OPERATION", "Time (nanoseconds)");
cout << LINE << endl;
printEntry(first.first, first.second);
cout << LINE << endl;
printEntry(last.first, last.second);
cout << LINE << endl;
printEntry(middle.first, middle.second);
cout << LINE << endl;
return 0;
}
/*
* name: printEntry
* purpose: prints a single line of table
* arguments: a string action (first entry in line)
* an int or string time (second entry in line)
* notes: uses table formatting constants defined at top of this file
*/
template<typename T>
void printEntry(const string action, const T time)
{
cout << left << setw(OP_ENTRY_WIDTH) << setfill(FILL) << action;
cout << left << setw(TIME_ENTRY_WIDTH) << setfill(FILL) << time << endl;
}
/*
* name: generateRandList
* purpose: generates a CharLinkedList of random elements
* arguments: const int size of list to generate
* returns: pointer to the new heap-allocated CharLinkedList
* effects: allocates CharLinkedList on heap
*/
CharLinkedList *generateRandList(const int size)
{
CharLinkedList *list = new CharLinkedList;
for(int i = 0; i < size; i++) {
// rand() will generate a random int. The mod operation will
// put it in range [0, 255]. In C++, ints translate into chars
// of the corresponding ASCII number.
list->pushAtBack(rand() % 256);
}
return list;
}
/*
* name: pushAtFront_test
* purpose: time how long it takes to push DEFAULT_NUM_OPS elements
* to front of list
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> pushAtFront_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
time_point<high_resolution_clock> start = high_resolution_clock::now();
for (int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->pushAtFront(DUMMY_CHAR);
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("pushAtFront " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}
/*
* name: pushAtBack_test
* purpose: time how long it takes to push DEFAULT_NUM_OPS elements
* to back of list
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> pushAtBack_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
time_point<high_resolution_clock> start = high_resolution_clock::now();
for (int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->pushAtBack(DUMMY_CHAR);
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("pushAtBack " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}
/*
* name: insertAtMiddle_test
* purpose: time how long it takes to insert DEFAULT_NUM_OPS elements
* into middle of list
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> insertAtMiddle_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
// keep track of list size separately so we don't
// rely on calling size() function, which could impact time complexity
int curr_size = DEFAULT_LIST_SIZE;
time_point<high_resolution_clock> start = high_resolution_clock::now();
for (int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->insertAt(DUMMY_CHAR, curr_size / 2);
curr_size++;
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("insertAt middle of list " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}
/*
* name: first_test
* purpose: time how long it takes to call first() on list DEFAULT_NUM_OPS times
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> first_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
// For each list access test, calling once before timing
// to control for caching.
list->first();
time_point<high_resolution_clock> start = high_resolution_clock::now();
for (int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->first();
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("call first() " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}
/*
* name: first_test
* purpose: time how long it takes to call last() on list DEFAULT_NUM_OPS times
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> last_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
// For each list access test, calling once before timing
// to control for caching.
list->last();
time_point<high_resolution_clock> start = high_resolution_clock::now();
for (int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->last();
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("call last() " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}
/*
* name: elementAtMiddle_test
* purpose: time how long it takes to access middle list element
* DEFAULT_NUM_OPS times
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> elementAtMiddle_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
// For each list access test, calling once before timing
// to control for caching.
list->elementAt(DEFAULT_LIST_SIZE / 2);
time_point<high_resolution_clock> start = high_resolution_clock::now();
for(int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->elementAt(DEFAULT_LIST_SIZE / 2);
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("call elementAt() for middle of list " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}
/*
* name: popFromFront_test
* purpose: time how long it takes to pop DEFAULT_NUM_OPS elements
* from front of list
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> popFromFront_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
time_point<high_resolution_clock> start = high_resolution_clock::now();
for (int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->popFromFront();
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("popFromFront " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}
/*
* name: popFromBack_test
* purpose: time how long it takes to pop DEFAULT_NUM_OPS elements
* from back of list
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> popFromBack_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
time_point<high_resolution_clock> start = high_resolution_clock::now();
for (int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->popFromBack();
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("popFromBack " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}
/*
* name: removeAtMiddle_test
* purpose: time how long it takes to remove DEFAULT_NUM_OPS elements
* from middle of list
* returns: pair<string, int> where first element is an operation description
* for the table, and second element is operaiton time in nanoseconds
*/
pair<string, int> removeAtMiddle_test()
{
CharLinkedList *list = generateRandList(DEFAULT_LIST_SIZE);
// keep track of list size separately so we don't
// rely on calling size() function, which could impact time complexity
int curr_size = DEFAULT_LIST_SIZE;
time_point<high_resolution_clock> start = high_resolution_clock::now();
for (int i = 0; i < DEFAULT_NUM_OPS; i++) {
list->removeAt(curr_size / 2);
curr_size--;
}
time_point<high_resolution_clock> stop = high_resolution_clock::now();
pair<string, int> res("removeAt middle of list " +
to_string(DEFAULT_NUM_OPS) + " times",
duration_cast<nanoseconds>(stop - start).count());
delete list;
return res;
}