-
Notifications
You must be signed in to change notification settings - Fork 0
/
BallRunner.java
134 lines (117 loc) · 3.66 KB
/
BallRunner.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
import java.util.*;
class BallRunner {
int numIterations = 10, numBins = 12, numElements=10;
int[] countsArr;
Ball[] packedArr, BFarr/*, JAarr*/;
Ball[][] unpackedArr,JAarr;
long exhaustiveTime, jaggedTime, jaggedTimeC;
Random rand = new Random();
//OJArrays OJA = new OJArrays();
JaggedBallArray jaggedArr = new JaggedBallArray(12, 10); // 12 bins, 10 elements
// randomly fill 2d unpackedarr with balls
public void randomize() {
countsArr = new int[numBins];
packedArr = new Ball[numElements];
for (int i = 0; i < numElements; i++) {
int pos = rand.nextInt(numBins);
countsArr[pos]++;
packedArr[i] = new Ball(pos);
}
unpackedArr = new Ball[numBins][];
System.out.println("Originals:");
for (Ball b : packedArr) {
System.out.print(b.toString() + " ");
}
System.out.println();
//build unpackedArr
for (int i = 0; i < countsArr.length; i++) {
if (countsArr[i] != 0) {
unpackedArr[i] = new Ball[countsArr[i]];
for (int j = 0; j < countsArr[i]; j++) {
unpackedArr[i][j] = new Ball(i);
}
} else {
unpackedArr[i] = null;
}
}
}
public void testBruteForce() {
long startTime = System.nanoTime();
ExhaustiveApproach approach1 = new ExhaustiveApproach();
BFarr = approach1.collide(packedArr);
for (int i = 0; i < numIterations-1; i++) {
BFarr = approach1.collide(BFarr);
// System.out.println("");
}
exhaustiveTime = System.nanoTime() - startTime;
System.out.println("Exhaustive method time for "+numIterations+" iterations: " + (exhaustiveTime / 1000000.0) + " ms");
}
public void testJaggedUP() {
// JAarrPacked = new Ball[unpackedArr.length];
long startTime = System.nanoTime();
JAarr = jaggedArr.collideUP(unpackedArr);
//countsArr = jaggedArr.getCounts();
for (int i = 0; i < numIterations-1; i++) {
JAarr = jaggedArr.collideUP(JAarr);
//countsArr = jaggedArr.getCounts();
}
/* int c = 0;
for (int i = 0; i < JAarr.length; i++) {
if (JAarr[i] != null) {
for (int j = 0; j < JAarr[i].length; j++) {
JAarrPacked[c] = JAarr[i][j];
c++;
}
}
}*/
jaggedTime = System.nanoTime() - startTime;
System.out.println("Jagged Array time USING UNPACKED for "+numIterations+" iterations: " + (jaggedTime / 1000000.0) + " ms");
}
public void testJaggedC(){
long startTime = System.nanoTime();
countsArr = jaggedArr.collideC(countsArr);
for (int i = 0; i < numIterations-1; i++) {
countsArr = jaggedArr.collideC(countsArr);
/*boolean stopped=true;
for(int c : countsArr){
if(c>1){
stopped=false;
}
}
if(stopped){
System.out.println("BALLS STOPPED AFTER "+i+" ITERATIONS");
break;
}*/
}
jaggedTimeC = System.nanoTime() - startTime;
System.out.println("Jagged Array time USING COUNTS for "+numIterations+" iterations: " + (jaggedTime / 1000000.0) + " ms");
}
public void printAll() {
System.out.println("Exhaustive:");
for (Ball b : BFarr) {
if (b != null) {
System.out.print(b.toString() + " ");
}
}
System.out.println("\nJagged Unpacked:");
for (Ball[] bArr : JAarr) {
if (bArr != null) {
for(Ball b : bArr){
System.out.print(b.toString() + " ");
}
}
}
System.out.println("\nJagged Counts:");
for(int c : countsArr){
System.out.print(c+" ");
}
}
public void run() {
// System.out.println("Randomly assigning ball locations...");
randomize();
testBruteForce();
testJaggedUP();
testJaggedC();
printAll();
}
}