-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreebench.java
175 lines (155 loc) · 6.1 KB
/
treebench.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
import java.util.Arrays;
class Tree {
}
class Node extends Tree {
public Tree left;
public Tree right;
Node(Tree l, Tree r) {
this.left = l;
this.right = r;
}
};
class Leaf extends Tree {
public long elem;
Leaf(long e) {
this.elem = e;
}
};
public class treebench {
private static Tree buildTreeHelper(int n, long root) {
if(n == 0) {
return new Leaf(root);
} else {
return new Node(buildTreeHelper(n-1, root),
buildTreeHelper(n-1, root + (1<<(n-1))));
}
}
private static Tree buildTreeHelper2(int n, long root) {
if(n == 0) {
return new Leaf(root);
} else {
return new Node(buildTreeHelper2(n-1, root),
buildTreeHelper2(n-1, root));
}
}
public static Tree buildTree(int n) {
return buildTreeHelper(n, 1);
}
public static Tree buildTree2(int n) {
return buildTreeHelper2(n, 1);
}
public static Tree add1Tree(Tree t) {
if (t instanceof Leaf) {
return new Leaf(((Leaf)t).elem + 1);
} else {
Node n = (Node)t;
return new Node(add1Tree(n.left),
add1Tree(n.right));
}
}
public static long sumTree(Tree t) {
if (t instanceof Leaf) {
return ((Leaf)t).elem;
} else {
Node n = (Node)t;
return sumTree(n.left) + sumTree(n.right);
}
}
public static void main(String[] args) {
System.out.println("Starting.");
String which = args[0];
int depth = Integer.parseInt(args[1]);
final int numTrials = Integer.parseInt(args[2]);
if(which.compareTo("build") == 0) {
long[] trials = new long[numTrials];
System.out.printf("Timing build tree\n");
System.out.printf("But first, %d warm-up iters.\n", depth, numTrials);
for(int i=0; i<numTrials; i++) {
final long startTime = System.currentTimeMillis();
Tree t2 = buildTree2(depth);
final long endTime = System.currentTimeMillis();
if(numTrials <= 500)
System.out.print(" " + (endTime - startTime));
}
System.out.printf("\nNow for the runs we'll count:\n");
final long prebatch = System.currentTimeMillis();
for(int i=0; i<numTrials; i++) {
final long startTime = System.currentTimeMillis();
Tree t2 = buildTree(depth);
final long endTime = System.currentTimeMillis();
// Todo: use something in t2 just to be sure.
trials[i] = (endTime - startTime);
if(numTrials <= 500)
System.out.print(" " + (endTime - startTime));
}
final long postbatch = System.currentTimeMillis();
System.out.printf("\nBATCHTIME: " + ((double)(postbatch - prebatch) / 1000) + "\n");
Arrays.sort(trials);
System.out.println("\n\nMEDIANTIMED: " + Double.toString((double)trials[numTrials/2] / 1000.0));
double sum = 0.0;
for (double d : trials) sum += d;
System.out.println("MEANTIMED: " + Double.toString(sum / (double)numTrials / 1000.0));
} else if(which.compareTo("sum") == 0) {
Tree t1 = buildTree(depth);
System.out.printf("Input tree built, depth %d. Running %d iters.\n", depth, numTrials);
long[] trials = new long[numTrials];
System.out.printf("But first, %d warm-up iters.\n", depth, numTrials);
for(int i=0; i<numTrials; i++) {
final long startTime = System.currentTimeMillis();
long t2 = sumTree(t1);
final long endTime = System.currentTimeMillis();
if(numTrials <= 500)
System.out.print(" " + (endTime - startTime));
}
System.out.printf("\nNow for the runs we'll count:\n");
final long prebatch = System.currentTimeMillis();
for(int i=0; i<numTrials; i++) {
final long startTime = System.currentTimeMillis();
long t2 = sumTree(t1);
final long endTime = System.currentTimeMillis();
// Todo: use something in t2 just to be sure.
trials[i] = (endTime - startTime);
if(numTrials <= 500)
System.out.print(" " + (endTime - startTime));
}
final long postbatch = System.currentTimeMillis();
System.out.printf("\nBATCHTIME: " + ((double)(postbatch - prebatch) / 1000) + "\n");
Arrays.sort(trials);
System.out.println("\n\nMEDIANTIMED: " + Double.toString((double)trials[numTrials/2] / 1000.0));
double sum = 0.0;
for (double d : trials) sum += d;
System.out.println("MEANTIMED: " + Double.toString(sum / (double)numTrials / 1000.0));
}
else {
Tree t1 = buildTree(depth);
System.out.printf("Input tree built, depth %d. Running %d iters.\n", depth, numTrials);
long[] trials = new long[numTrials];
System.out.printf("But first, %d warm-up iters.\n", depth, numTrials);
for(int i=0; i<numTrials; i++) {
final long startTime = System.currentTimeMillis();
Tree t2 = add1Tree(t1);
final long endTime = System.currentTimeMillis();
if(numTrials <= 500)
System.out.print(" " + (endTime - startTime));
}
System.out.printf("\nNow for the runs we'll count:\n");
final long prebatch = System.currentTimeMillis();
for(int i=0; i<numTrials; i++) {
final long startTime = System.currentTimeMillis();
Tree t2 = add1Tree(t1);
final long endTime = System.currentTimeMillis();
// Todo: use something in t2 just to be sure.
trials[i] = (endTime - startTime);
if(numTrials <= 500)
System.out.print(" " + (endTime - startTime));
}
final long postbatch = System.currentTimeMillis();
System.out.printf("\nBATCHTIME: " + ((double)(postbatch - prebatch) / 1000) + "\n");
Arrays.sort(trials);
System.out.println("\n\nMEDIANTIMED: " + Double.toString((double)trials[numTrials/2] / 1000.0));
double sum = 0.0;
for (double d : trials) sum += d;
System.out.println("MEANTIMED: " + Double.toString(sum / (double)numTrials / 1000.0));
}
}
}