This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RunTimeTestSeconds.java
310 lines (260 loc) · 10.7 KB
/
RunTimeTestSeconds.java
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
import edu.colorado.collections.IntArrayBag;
import edu.colorado.collections.IntLinkedBag;
import java.util.Random;
/**
* Times how long it takes different methods to run for IntArrayBag and
* for IntLinkedBag under different sizes of Bags.
* @author CIS112
*
* <A HREF="mailto:[email protected]"> ([email protected]) </A>
* @version
* September 16, 2011
**/
public class RunTimeTestSeconds {
/**
*
*/
//declare references to the bag objects used for testing
private IntArrayBag testIABag;
private IntArrayBag unionBag;
private IntLinkedBag testILBag;
private IntLinkedBag unionILBag;
//var to hold the number of times to run test, is also used to average the runtimer1
private int numIterations = 100;
//String array to automate chart printing
private String[] methods = {"constructor", "countOccurance","getCapacity","remove","size","trimToSize","union"};
//2d arrays to hold runtimer1 & 2 average results, again used to automate
private long [][] resultsIAB = new long [7][5];
private long [][] resultsILB = new long [7][5];
//array to hold the size of the collection ADT
private int[] numElements ={100,1000,10000,100000,1000000};
//timer references
private long startTime = 0;
private float runtimer1 = 0;
private float runtimer2 = 0;
///random num generator
Random rand = new Random();
/**
* Main method to instantiate a RuntimeTest and call go
* @param args
* the command line arguments
*/
public static void main(String[] args)
{
RuntimeTest newTest = new RuntimeTest();
newTest.go();
}
/**
* Runs the test
* @param - none
*/
public void go()
{
System.out.println("Running Test....");
//for loop to construct n-size bags and run benchmark testing
int a = 0;
for(int size : numElements)
{
unionBag = new IntArrayBag(size);
unionBag.add(rand.nextInt(size));
testILBag = new IntLinkedBag();
unionILBag = new IntLinkedBag();
//for loop to run test multiple times to smooth data
for(int i = 0; i < numIterations; i++)
{
//get system counter
startTime = System.currentTimeMillis();
testIABag = new IntArrayBag(size);
//add elapsed time to runtimer1 var
runtimer1 += System.currentTimeMillis() - startTime;
}
//average over number iterations
float elapsedTimeSec = runtimer1/1000F;
runtimer1 = elapsedTimeSec/numIterations;
resultsIAB[0][a] = (long) runtimer1;
resultsILB[0][a] =(long) Double.NaN;
//fill the bags for testing, this operation does not require data reduction
for(int i = 0; i <= size; i++)
{
testIABag.add(rand.nextInt(size));
testILBag.add(rand.nextInt(size));
unionILBag.add(rand.nextInt(size));
}
///test time of countOccurance()
runtimer1 = 0;
runtimer2 = 0;
for(int i = 0; i < numIterations; i++)
{
startTime = System.currentTimeMillis();
testIABag.countOccurrences(rand.nextInt(size));
runtimer1 += System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();
testILBag.countOccurrences(rand.nextInt(size));
runtimer2 += System.currentTimeMillis() - startTime;
}
elapsedTimeSec = runtimer1/1000F;
float elapsedTimeSec2 = runtimer2/1000F;
runtimer1 = elapsedTimeSec/numIterations;
runtimer2 = elapsedTimeSec2/numIterations;
resultsIAB[1][a] = (long) ( runtimer1);
resultsILB[1][a] = (long) ( runtimer2);
//test time of getCapacity
runtimer1 = 0;
runtimer2 = 0;
for(int i = 0; i < numIterations; i++)
{
startTime = System.currentTimeMillis();
testIABag.getCapacity();
runtimer1 += System.currentTimeMillis() - startTime;
}
elapsedTimeSec = runtimer1/1000F;
elapsedTimeSec = runtimer2/1000F;
runtimer1 = elapsedTimeSec/numIterations;
resultsIAB[2][a] = (long) ( runtimer1);
resultsILB[2][a] = (long) Double.NaN;
//test time of remove
runtimer1 = 0;
runtimer2 = 0;
for(int i = 0; i < numIterations; i++)
{
startTime = System.currentTimeMillis();
testIABag.remove(rand.nextInt(size));
runtimer1 += System.currentTimeMillis() - startTime;
if(testIABag.size() <testIABag.getCapacity());
{
testIABag.add(rand.nextInt(size));
}
startTime = System.currentTimeMillis();
testILBag.remove(rand.nextInt(size));
runtimer2 += System.currentTimeMillis() - startTime;
if(testILBag.size() < size)
{
testILBag.add(rand.nextInt(size));
}
}
elapsedTimeSec = runtimer1/1000F;
elapsedTimeSec2 = runtimer2/1000F;
runtimer1 = elapsedTimeSec/numIterations;
runtimer2 = elapsedTimeSec2/numIterations;
resultsIAB[3][a] = (long) ( runtimer1/numIterations);
resultsILB[3][a] = (long) ( runtimer2/numIterations);
// time of size method
runtimer1 = 0;
runtimer2 = 0;
for(int i = 0; i < numIterations; i++)
{
startTime = System.currentTimeMillis();
testIABag.size();
runtimer1 += System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();
testILBag.size();
runtimer2 += System.currentTimeMillis() - startTime;
}
elapsedTimeSec = runtimer1/1000F;
elapsedTimeSec2 = runtimer2/1000F;
runtimer1 = elapsedTimeSec/numIterations;
runtimer2 = elapsedTimeSec2/numIterations;
resultsIAB[4][a] = (long) ( runtimer1/numIterations);
resultsILB[4][a] = (long) ( runtimer2/numIterations);
//time of trimToSizeMethod
// trim re-instantiates the bag and fills it to a random size
//this is done to simulate different trim times to get an average case
runtimer1 = 0;
for(int i = 0; i < numIterations; i++)
{
testIABag = new IntArrayBag(size);
//generate random sizes to trim to
intArrayBagFill(testIABag,rand.nextInt(size), size );
startTime = System.currentTimeMillis();
testIABag.trimToSize();
runtimer1 += System.currentTimeMillis() - startTime;
}
elapsedTimeSec = runtimer1/1000F;
runtimer1 = elapsedTimeSec/numIterations;
resultsIAB[5][a] = ((int)runtimer1/numIterations);
resultsILB[5][a] = (long) Double.NaN;
runtimer1 = 0;
//test time of union
for(int i = 0; i < numIterations; i++)
{
startTime = System.currentTimeMillis();
//union is static so it is called with the "class." syntax
IntArrayBag.union(testIABag, unionBag);
runtimer1 += System.currentTimeMillis() - startTime;
startTime = System.currentTimeMillis();
IntLinkedBag.union(testILBag, unionILBag);
runtimer2 += System.currentTimeMillis() - startTime;
}
elapsedTimeSec = runtimer1/1000F;
elapsedTimeSec2 = runtimer2/1000F;
runtimer1 = elapsedTimeSec/numIterations;
runtimer2 = elapsedTimeSec2/numIterations;
resultsIAB[6][a] = (long) ( runtimer1/numIterations);
resultsILB[6][a] = (long) ( runtimer2/numIterations);
a++;//increment results array counter
}//end of IAB test loop
// Print results to the standard IO
print();
}
/**
* Prints results to the standard IO
* @param - none
* @postcondition
* results are printed to the standard IO
*/
public void print()
{
//sets up column headers
System.out.printf("%15s", " ");
for(int size : numElements)
{
System.out.printf("%15s",size);
}
System.out.println("\nIAB");
// Print IAB results
int m = 0;
for(long[] r :resultsIAB)
{
System.out.printf("%-15s", methods[m]);
for(long c : r)
{
System.out.printf("%15s", c);
}
System.out.println();
m++;
}
// Print ILB results
System.out.println("\nILB");
int n = 0;
for(long[] r :resultsILB)
{
System.out.printf("%-15s", methods[n]);
for(long c : r)
{
System.out.printf("%15s", c);
}
System.out.println();
n++;
}
}
/**
* Fill a bag with random integers
* @param bag
* the IntArrayBag that is to be added to
* @param howmany
* the number of integers to add
* @param
* the range(maximum) value of an integers allowed
*
* @postcondition
* The bag has had the specified number of integers added
*
**/
public void intArrayBagFill(IntArrayBag bag,int howMany, int range)
{
for(int i = 0; i <= howMany; i++)
{
bag.add(rand.nextInt(range));
}
}
}