-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAQueue_tests.cs
More file actions
301 lines (261 loc) · 12.3 KB
/
Copy pathAQueue_tests.cs
File metadata and controls
301 lines (261 loc) · 12.3 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
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
using System;
using System.Diagnostics;
public class AQueueTests
{
public static void RunTests()
{
AQueue<int> intAQueue = new AQueue<int>(10);
// Enqueue Tests
Console.WriteLine("Enqueue Test");
for (int i = 1; i <= 6; i++) {
intAQueue.Enqueue(i);
}
Console.WriteLine("Output");
intAQueue.Display(); // Expected: 1 2 3 4 5 6
Console.WriteLine();
// Dequeue Tests
Console.WriteLine("Dequeue Test");
Console.WriteLine(intAQueue.Dequeue()); // Expected: 1
Console.WriteLine(intAQueue.Dequeue()); // Expected: 2
Console.WriteLine("Display");
intAQueue.Display(); // Expected: 3 4 5 6
Console.WriteLine();
// Peek Tests
Console.WriteLine("Peek Test");
Console.WriteLine(intAQueue.Peek()); // Expected: 3
Console.WriteLine("Display");
intAQueue.Display(); // Expected: 3 4 5 6
Console.WriteLine();
// Contains Tests
Console.WriteLine("Contains Test");
Console.WriteLine(intAQueue.Contains(5) ? "5: True" : "5: False"); // Expected: 5: True
Console.WriteLine(intAQueue.Contains(7) ? "7: True" : "7: False"); // Expected: 7: False
Console.WriteLine();
// Edge Cases
Console.WriteLine("Edge Case - Dequeue from empty queue");
while (intAQueue.GetSize() > 0) {
intAQueue.Dequeue();
}
intAQueue.Display();
// Comment this out to proceed
// intAQueue.Dequeue();
// Console.WriteLine();
Console.WriteLine("Edge Case - Peek from empty queue");
// Comment this out to proceed
// intAQueue.Peek();
// Console.WriteLine();
Console.WriteLine("Edge Case - Queue operations continue as normal when pointers wrap around");
// Since our set capacity is 10, the first enqueue tests brought the pointers to be at 6
// This for loop will keep _front at 6 and wrap _rear around to 2
for (int i = 1; i < 6; i++) {
intAQueue.Enqueue(i);
}
intAQueue.Display();
Console.WriteLine();
// // Performance Tests
// AQueue<int> stopwatchAQueue = new AQueue<int>(50_001);
// const int iterations = 1_000;
// const int iterationSize = 50;
// // Enqueue Performance
// TimeSpan totalEnqueueTime = TimeSpan.Zero;
// // This loop will run 1,000 times to get a good spread of data to average
// for (int i = 1; i <= iterations; i++) {
// // This loop will report the total time to complete 50 Enqueue calls
// var startEnqueue = Stopwatch.GetTimestamp();
// for (int j = 1; j <= iterationSize; j++) {
// stopwatchAQueue.Enqueue(j);
// }
// totalEnqueueTime += Stopwatch.GetElapsedTime(startEnqueue);
// }
// // Find the average of the sets of 50 Enqueue calls
// TimeSpan enqueueAvg = totalEnqueueTime / (iterations + 1);
// Console.WriteLine($"Average Enqueue Time per 50: {enqueueAvg.TotalNanoseconds} nanoseconds");
// // Each of the following method performance tests are set up the same as Enqueue to
// // ensure we have a good metric to compare each method against each other
// // Contains Performance
// TimeSpan totalContainsTime = TimeSpan.Zero;
// for (int i = 1; i <= iterations; i ++) {
// var startContains = Stopwatch.GetTimestamp();
// for (int j = 1; j <= iterationSize; j++) {
// stopwatchAQueue.Contains(j);
// }
// totalContainsTime += Stopwatch.GetElapsedTime(startContains);
// }
// TimeSpan containsAvg = totalContainsTime / (iterations + 1);
// Console.WriteLine($"Average Contains Time per 50: {containsAvg.TotalNanoseconds} nanoseconds");
// Console.WriteLine($"Average Contains Timer per 50: {containsAvg.TotalSeconds} seconds");
// // Peek Performance
// TimeSpan totalPeekTime = TimeSpan.Zero;
// for (int i = 1; i <= iterations; i ++) {
// var startPeek = Stopwatch.GetTimestamp();
// for (int j = 1; j <= iterationSize; j++) {
// stopwatchAQueue.Peek();
// }
// totalPeekTime += Stopwatch.GetElapsedTime(startPeek);
// }
// TimeSpan peekAvg = totalPeekTime / (iterations + 1);
// Console.WriteLine($"Average Peek Time per 50: {peekAvg.TotalNanoseconds} nanoseconds");
// // Dequeue Performance
// TimeSpan totalDequeueTime = TimeSpan.Zero;
// int totalDequeues = iterations * iterationSize;
// if (stopwatchAQueue.GetSize() < totalDequeues) {
// throw new InvalidOperationException("Not enough items to dequeue.");
// }
// for (int i = 1; i <= iterations; i ++) {
// var startDequeue = Stopwatch.GetTimestamp();
// for (int j = 1; j <= iterationSize; j++) {
// stopwatchAQueue.Dequeue();
// }
// totalDequeueTime += Stopwatch.GetElapsedTime(startDequeue);
// }
// TimeSpan dequeueAvg = totalDequeueTime / (iterations + 1);
// Console.WriteLine($"Average Dequeue Time per 50: {dequeueAvg.TotalNanoseconds} nanoseconds");
AQueue<int> stopwatchAQueue = new AQueue<int>(51_000_000);
const int iterations1 = 10;
const int iterations2 = 1_000;
const int iterations3 = 1_000_000;
const int iterationSize = 50;
// Helper method to prefill the queue
void PrefillQueue(AQueue<int> queue, int count) {
for (int i = 0; i < count; i++) {
queue.Enqueue(i);
}
}
// Enqueue
TimeSpan totalEnqueueTime1 = TimeSpan.Zero;
for (int i = 0; i < iterations1; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Enqueue(j);
}
totalEnqueueTime1 += Stopwatch.GetElapsedTime(start);
}
TimeSpan enqueueAvg10 = totalEnqueueTime1 / iterations1;
TimeSpan totalEnqueueTime2 = TimeSpan.Zero;
for (int i = 0; i < iterations2; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Enqueue(j);
}
totalEnqueueTime2 += Stopwatch.GetElapsedTime(start);
}
TimeSpan enqueueAvg1000 = totalEnqueueTime2 / iterations2;
TimeSpan totalEnqueueTime3 = TimeSpan.Zero;
for (int i = 0; i < iterations3; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Enqueue(j);
}
totalEnqueueTime3 += Stopwatch.GetElapsedTime(start);
}
TimeSpan enqueueAvg1000000 = totalEnqueueTime3 / iterations3;
// Contains
TimeSpan totalContainsTime1 = TimeSpan.Zero;
for (int i = 0; i < iterations1; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Contains(j);
}
totalContainsTime1 += Stopwatch.GetElapsedTime(start);
}
TimeSpan containsAvg10 = totalContainsTime1 / iterations1;
TimeSpan totalContainsTime2 = TimeSpan.Zero;
for (int i = 0; i < iterations2; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Contains(j);
}
totalContainsTime2 += Stopwatch.GetElapsedTime(start);
}
TimeSpan containsAvg1000 = totalContainsTime2 / iterations2;
TimeSpan totalContainsTime3 = TimeSpan.Zero;
for (int i = 0; i < iterations3; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Contains(j);
}
totalContainsTime3 += Stopwatch.GetElapsedTime(start);
}
TimeSpan containsAvg1000000 = totalContainsTime3 / iterations3;
// Peek
TimeSpan totalPeekTime1 = TimeSpan.Zero;
for (int i = 0; i < iterations1; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Peek();
}
totalPeekTime1 += Stopwatch.GetElapsedTime(start);
}
TimeSpan peekAvg10 = totalPeekTime1 / iterations1;
TimeSpan totalPeekTime2 = TimeSpan.Zero;
for (int i = 0; i < iterations2; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Peek();
}
totalPeekTime2 += Stopwatch.GetElapsedTime(start);
}
TimeSpan peekAvg1000 = totalPeekTime2 / iterations2;
TimeSpan totalPeekTime3 = TimeSpan.Zero;
for (int i = 0; i < iterations3; i++) {
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Peek();
}
totalPeekTime3 += Stopwatch.GetElapsedTime(start);
}
TimeSpan peekAvg1000000 = totalPeekTime3 / iterations3;
// Dequeue
TimeSpan totalDequeueTime1 = TimeSpan.Zero;
for (int i = 0; i < iterations1; i++) {
PrefillQueue(stopwatchAQueue, iterationSize);
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Dequeue();
}
totalDequeueTime1 += Stopwatch.GetElapsedTime(start);
}
TimeSpan dequeueAvg10 = totalDequeueTime1 / iterations1;
TimeSpan totalDequeueTime2 = TimeSpan.Zero;
for (int i = 0; i < iterations2; i++) {
PrefillQueue(stopwatchAQueue, iterationSize);
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Dequeue();
}
totalDequeueTime2 += Stopwatch.GetElapsedTime(start);
}
TimeSpan dequeueAvg1000 = totalDequeueTime2 / iterations2;
TimeSpan totalDequeueTime3 = TimeSpan.Zero;
for (int i = 0; i < iterations3; i++) {
PrefillQueue(stopwatchAQueue, iterationSize);
var start = Stopwatch.GetTimestamp();
for (int j = 0; j < iterationSize; j++) {
stopwatchAQueue.Dequeue();
}
totalDequeueTime3 += Stopwatch.GetElapsedTime(start);
}
TimeSpan dequeueAvg1000000 = totalDequeueTime3 / iterations3;
// Display Results
Console.WriteLine("Performance Results for AQueue (Average Time per 50 runs in Ticks)");
Console.WriteLine("-------------------------------------------------------------------");
Console.WriteLine("| Operation | 10 | 1,000 | 1,000,000 |");
Console.WriteLine("-------------------------------------------------------------------");
Console.WriteLine($"| Enqueue | {enqueueAvg10.Ticks,8} | {enqueueAvg1000.Ticks,8} | {enqueueAvg1000000.Ticks,9} |");
Console.WriteLine($"| Contains | {containsAvg10.Ticks,8} | {containsAvg1000.Ticks,8} | {containsAvg1000000.Ticks,9} |");
Console.WriteLine($"| Peek | {peekAvg10.Ticks,8} | {peekAvg1000.Ticks,8} | {peekAvg1000000.Ticks,9} |");
Console.WriteLine($"| Dequeue | {dequeueAvg10.Ticks,8} | {dequeueAvg1000.Ticks,8} | {dequeueAvg1000000.Ticks,9} |");
Console.WriteLine("-------------------------------------------------------------------");
}
}
/*
Analysis
I expected Enqueue to be very quick, along with Dequeue and Peek. When I ran these tests with
50,000,000 total iterations, the average time for each of these stayed almost exactly the same
as the current 50,000 total iterations. Contains on the other hand, I expected to take a while,
being O(n). When I ran Contains with the 50,000,000 total iterations, it took so long to
complete that my computer fell asleep. I decided to change the total iteration count to 50,000
which drastically cut down the time to complete the Contains tests. Still, the total time to
complete all the Contains tests takes long enough to make me wonder if I left an infinite loop
somewhere in the code.
*/