1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.10 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.2 |
*/ |
6 |
dl |
1.1 |
/** |
7 |
|
|
* @test |
8 |
|
|
* @synopsis Times and checks basic map operations |
9 |
|
|
*/ |
10 |
jsr166 |
1.13 |
import java.io.*; |
11 |
dl |
1.1 |
import java.util.*; |
12 |
|
|
|
13 |
|
|
public class NavigableMapCheck { |
14 |
|
|
|
15 |
|
|
static int absentSize; |
16 |
|
|
static int absentMask; |
17 |
|
|
static Integer[] absent; |
18 |
|
|
|
19 |
|
|
static final Integer MISSING = new Integer(Integer.MIN_VALUE); |
20 |
|
|
|
21 |
|
|
static TestTimer timer = new TestTimer(); |
22 |
|
|
|
23 |
|
|
static void reallyAssert(boolean b) { |
24 |
|
|
if (!b) throw new Error("Failed Assertion"); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
public static void main(String[] args) throws Exception { |
28 |
jsr166 |
1.12 |
Class<?> mapClass = null; |
29 |
dl |
1.1 |
int numTests = 50; |
30 |
|
|
int size = 50000; |
31 |
|
|
|
32 |
|
|
if (args.length > 0) { |
33 |
|
|
try { |
34 |
|
|
mapClass = Class.forName(args[0]); |
35 |
jsr166 |
1.6 |
} catch (ClassNotFoundException e) { |
36 |
dl |
1.1 |
throw new RuntimeException("Class " + args[0] + " not found."); |
37 |
|
|
} |
38 |
|
|
} |
39 |
|
|
|
40 |
jsr166 |
1.5 |
if (args.length > 1) |
41 |
dl |
1.1 |
numTests = Integer.parseInt(args[1]); |
42 |
|
|
|
43 |
jsr166 |
1.5 |
if (args.length > 2) |
44 |
dl |
1.1 |
size = Integer.parseInt(args[2]); |
45 |
|
|
|
46 |
|
|
System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size); |
47 |
|
|
|
48 |
|
|
absentSize = 8; |
49 |
|
|
while (absentSize < size) absentSize <<= 1; |
50 |
|
|
absentMask = absentSize - 1; |
51 |
|
|
absent = new Integer[absentSize]; |
52 |
|
|
|
53 |
jsr166 |
1.5 |
for (int i = 0; i < absentSize; ++i) |
54 |
dl |
1.1 |
absent[i] = new Integer(i * 2); |
55 |
|
|
|
56 |
|
|
Integer[] key = new Integer[size]; |
57 |
jsr166 |
1.5 |
for (int i = 0; i < size; ++i) |
58 |
dl |
1.1 |
key[i] = new Integer(i * 2 + 1); |
59 |
|
|
|
60 |
|
|
for (int rep = 0; rep < numTests; ++rep) { |
61 |
|
|
runTest(newMap(mapClass), key); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
TestTimer.printStats(); |
65 |
|
|
|
66 |
|
|
} |
67 |
|
|
|
68 |
jsr166 |
1.12 |
static NavigableMap newMap(Class<?> cl) { |
69 |
dl |
1.1 |
try { |
70 |
jsr166 |
1.6 |
NavigableMap m = (NavigableMap) cl.newInstance(); |
71 |
dl |
1.1 |
return m; |
72 |
jsr166 |
1.6 |
} catch (Exception e) { |
73 |
dl |
1.1 |
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
static void runTest(NavigableMap s, Integer[] key) { |
78 |
|
|
shuffle(key); |
79 |
|
|
int size = key.length; |
80 |
|
|
long startTime = System.currentTimeMillis(); |
81 |
|
|
test(s, key); |
82 |
|
|
long time = System.currentTimeMillis() - startTime; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
static void t1(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
86 |
|
|
int sum = 0; |
87 |
|
|
int iters = 4; |
88 |
|
|
timer.start(nm, n * iters); |
89 |
|
|
for (int j = 0; j < iters; ++j) { |
90 |
|
|
for (int i = 0; i < n; i++) { |
91 |
|
|
if (s.get(key[i]) != null) ++sum; |
92 |
|
|
} |
93 |
|
|
} |
94 |
jsr166 |
1.5 |
timer.finish(); |
95 |
jsr166 |
1.9 |
reallyAssert(sum == expect * iters); |
96 |
dl |
1.1 |
} |
97 |
|
|
|
98 |
|
|
static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
99 |
|
|
int sum = 0; |
100 |
|
|
timer.start(nm, n); |
101 |
|
|
for (int i = 0; i < n; i++) { |
102 |
|
|
if (s.remove(key[i]) != null) ++sum; |
103 |
|
|
} |
104 |
jsr166 |
1.5 |
timer.finish(); |
105 |
jsr166 |
1.9 |
reallyAssert(sum == expect); |
106 |
dl |
1.1 |
} |
107 |
|
|
|
108 |
|
|
static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
109 |
|
|
int sum = 0; |
110 |
|
|
timer.start(nm, n); |
111 |
|
|
for (int i = 0; i < n; i++) { |
112 |
|
|
if (s.put(key[i], absent[i & absentMask]) == null) ++sum; |
113 |
|
|
} |
114 |
jsr166 |
1.5 |
timer.finish(); |
115 |
jsr166 |
1.9 |
reallyAssert(sum == expect); |
116 |
dl |
1.1 |
} |
117 |
|
|
|
118 |
|
|
static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
119 |
|
|
int sum = 0; |
120 |
|
|
timer.start(nm, n); |
121 |
|
|
for (int i = 0; i < n; i++) { |
122 |
|
|
if (s.containsKey(key[i])) ++sum; |
123 |
|
|
} |
124 |
jsr166 |
1.5 |
timer.finish(); |
125 |
jsr166 |
1.9 |
reallyAssert(sum == expect); |
126 |
dl |
1.1 |
} |
127 |
|
|
|
128 |
|
|
static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
129 |
|
|
int sum = 0; |
130 |
|
|
timer.start(nm, n/2); |
131 |
|
|
for (int i = n-2; i >= 0; i-=2) { |
132 |
|
|
if (s.remove(key[i]) != null) ++sum; |
133 |
|
|
} |
134 |
jsr166 |
1.5 |
timer.finish(); |
135 |
jsr166 |
1.9 |
reallyAssert(sum == expect); |
136 |
dl |
1.1 |
} |
137 |
|
|
|
138 |
|
|
static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { |
139 |
|
|
int sum = 0; |
140 |
|
|
timer.start(nm, n * 2); |
141 |
|
|
for (int i = 0; i < n; i++) { |
142 |
|
|
if (s.get(k1[i]) != null) ++sum; |
143 |
|
|
if (s.get(k2[i & absentMask]) != null) ++sum; |
144 |
|
|
} |
145 |
jsr166 |
1.5 |
timer.finish(); |
146 |
jsr166 |
1.9 |
reallyAssert(sum == n); |
147 |
dl |
1.1 |
} |
148 |
|
|
|
149 |
|
|
static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { |
150 |
|
|
int sum = 0; |
151 |
|
|
timer.start(nm, n * 2); |
152 |
|
|
for (int i = 0; i < n; i++) { |
153 |
|
|
if (s.containsKey(k1[i])) ++sum; |
154 |
|
|
if (s.containsKey(k2[i & absentMask])) ++sum; |
155 |
|
|
} |
156 |
jsr166 |
1.5 |
timer.finish(); |
157 |
jsr166 |
1.9 |
reallyAssert(sum == n); |
158 |
dl |
1.1 |
} |
159 |
|
|
|
160 |
|
|
static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
161 |
|
|
int sum = 0; |
162 |
|
|
timer.start(nm, n); |
163 |
|
|
for (int i = 0; i < n; i++) { |
164 |
|
|
if (s.get(key[i]) != null) ++sum; |
165 |
|
|
} |
166 |
jsr166 |
1.5 |
timer.finish(); |
167 |
jsr166 |
1.9 |
reallyAssert(sum == expect); |
168 |
dl |
1.1 |
} |
169 |
|
|
|
170 |
|
|
static void t9(NavigableMap s) { |
171 |
|
|
int sum = 0; |
172 |
|
|
int iters = 20; |
173 |
|
|
timer.start("ContainsValue (/n) ", iters * s.size()); |
174 |
|
|
int step = absentSize / iters; |
175 |
|
|
for (int i = 0; i < absentSize; i += step) |
176 |
|
|
if (s.containsValue(absent[i])) ++sum; |
177 |
jsr166 |
1.5 |
timer.finish(); |
178 |
jsr166 |
1.9 |
reallyAssert(sum != 0); |
179 |
dl |
1.1 |
} |
180 |
|
|
|
181 |
|
|
static void higherTest(NavigableMap s) { |
182 |
|
|
int sum = 0; |
183 |
|
|
int iters = s.size(); |
184 |
|
|
timer.start("Higher ", iters); |
185 |
|
|
Map.Entry e = s.firstEntry(); |
186 |
|
|
while (e != null) { |
187 |
|
|
++sum; |
188 |
|
|
e = s.higherEntry(e.getKey()); |
189 |
|
|
} |
190 |
jsr166 |
1.5 |
timer.finish(); |
191 |
jsr166 |
1.9 |
reallyAssert(sum == iters); |
192 |
dl |
1.1 |
} |
193 |
|
|
|
194 |
|
|
static void lowerTest(NavigableMap s) { |
195 |
|
|
int sum = 0; |
196 |
|
|
int iters = s.size(); |
197 |
|
|
timer.start("Lower ", iters); |
198 |
|
|
Map.Entry e = s.firstEntry(); |
199 |
|
|
while (e != null) { |
200 |
|
|
++sum; |
201 |
|
|
e = s.higherEntry(e.getKey()); |
202 |
|
|
} |
203 |
jsr166 |
1.5 |
timer.finish(); |
204 |
jsr166 |
1.9 |
reallyAssert(sum == iters); |
205 |
dl |
1.1 |
} |
206 |
|
|
|
207 |
|
|
static void ceilingTest(NavigableMap s) { |
208 |
|
|
int sum = 0; |
209 |
|
|
int iters = s.size(); |
210 |
|
|
if (iters > absentSize) iters = absentSize; |
211 |
|
|
timer.start("Ceiling ", iters); |
212 |
|
|
for (int i = 0; i < iters; ++i) { |
213 |
|
|
Map.Entry e = s.ceilingEntry(absent[i]); |
214 |
|
|
if (e != null) |
215 |
|
|
++sum; |
216 |
|
|
} |
217 |
jsr166 |
1.5 |
timer.finish(); |
218 |
jsr166 |
1.9 |
reallyAssert(sum == iters); |
219 |
dl |
1.1 |
} |
220 |
|
|
|
221 |
|
|
static void floorTest(NavigableMap s) { |
222 |
|
|
int sum = 0; |
223 |
|
|
int iters = s.size(); |
224 |
|
|
if (iters > absentSize) iters = absentSize; |
225 |
|
|
timer.start("Floor ", iters); |
226 |
|
|
for (int i = 1; i < iters; ++i) { |
227 |
|
|
Map.Entry e = s.floorEntry(absent[i]); |
228 |
|
|
if (e != null) |
229 |
|
|
++sum; |
230 |
|
|
} |
231 |
jsr166 |
1.5 |
timer.finish(); |
232 |
jsr166 |
1.9 |
reallyAssert(sum == iters-1); |
233 |
dl |
1.1 |
} |
234 |
|
|
|
235 |
|
|
static void ktest(NavigableMap s, int size, Integer[] key) { |
236 |
|
|
timer.start("ContainsKey ", size); |
237 |
|
|
Set ks = s.keySet(); |
238 |
|
|
int sum = 0; |
239 |
|
|
for (int i = 0; i < size; i++) { |
240 |
|
|
if (ks.contains(key[i])) ++sum; |
241 |
|
|
} |
242 |
jsr166 |
1.5 |
timer.finish(); |
243 |
jsr166 |
1.9 |
reallyAssert(sum == size); |
244 |
dl |
1.1 |
} |
245 |
|
|
|
246 |
|
|
static void ittest1(NavigableMap s, int size) { |
247 |
|
|
int sum = 0; |
248 |
|
|
timer.start("Iter Key ", size); |
249 |
|
|
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
250 |
jsr166 |
1.6 |
if (it.next() != MISSING) |
251 |
dl |
1.1 |
++sum; |
252 |
|
|
} |
253 |
jsr166 |
1.5 |
timer.finish(); |
254 |
jsr166 |
1.9 |
reallyAssert(sum == size); |
255 |
dl |
1.1 |
} |
256 |
|
|
|
257 |
|
|
static void ittest2(NavigableMap s, int size) { |
258 |
|
|
int sum = 0; |
259 |
|
|
timer.start("Iter Value ", size); |
260 |
|
|
for (Iterator it = s.values().iterator(); it.hasNext(); ) { |
261 |
jsr166 |
1.6 |
if (it.next() != MISSING) |
262 |
dl |
1.1 |
++sum; |
263 |
|
|
} |
264 |
jsr166 |
1.5 |
timer.finish(); |
265 |
jsr166 |
1.9 |
reallyAssert(sum == size); |
266 |
dl |
1.1 |
} |
267 |
|
|
static void ittest3(NavigableMap s, int size) { |
268 |
|
|
int sum = 0; |
269 |
|
|
timer.start("Iter Entry ", size); |
270 |
|
|
for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { |
271 |
jsr166 |
1.6 |
if (it.next() != MISSING) |
272 |
dl |
1.1 |
++sum; |
273 |
|
|
} |
274 |
jsr166 |
1.5 |
timer.finish(); |
275 |
jsr166 |
1.9 |
reallyAssert(sum == size); |
276 |
dl |
1.1 |
} |
277 |
|
|
|
278 |
|
|
static void ittest(NavigableMap s, int size) { |
279 |
|
|
ittest1(s, size); |
280 |
|
|
ittest2(s, size); |
281 |
|
|
ittest3(s, size); |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
static void rittest1(NavigableMap s, int size) { |
285 |
|
|
int sum = 0; |
286 |
|
|
timer.start("Desc Iter Key ", size); |
287 |
|
|
for (Iterator it = s.descendingKeySet().iterator(); it.hasNext(); ) { |
288 |
jsr166 |
1.6 |
if (it.next() != MISSING) |
289 |
dl |
1.1 |
++sum; |
290 |
|
|
} |
291 |
jsr166 |
1.5 |
timer.finish(); |
292 |
jsr166 |
1.9 |
reallyAssert(sum == size); |
293 |
dl |
1.1 |
} |
294 |
|
|
|
295 |
|
|
static void rittest(NavigableMap s, int size) { |
296 |
|
|
rittest1(s, size); |
297 |
dl |
1.3 |
// rittest2(s, size); |
298 |
dl |
1.1 |
} |
299 |
|
|
|
300 |
|
|
static void rtest(NavigableMap s, int size) { |
301 |
|
|
timer.start("Remove (iterator) ", size); |
302 |
|
|
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
303 |
|
|
it.next(); |
304 |
|
|
it.remove(); |
305 |
|
|
} |
306 |
jsr166 |
1.5 |
timer.finish(); |
307 |
dl |
1.1 |
} |
308 |
|
|
|
309 |
|
|
static void rvtest(NavigableMap s, int size) { |
310 |
|
|
timer.start("Remove (iterator) ", size); |
311 |
|
|
for (Iterator it = s.values().iterator(); it.hasNext(); ) { |
312 |
|
|
it.next(); |
313 |
|
|
it.remove(); |
314 |
|
|
} |
315 |
jsr166 |
1.5 |
timer.finish(); |
316 |
dl |
1.1 |
} |
317 |
|
|
|
318 |
|
|
static void dtest(NavigableMap s, int size, Integer[] key) { |
319 |
|
|
timer.start("Put (putAll) ", size * 2); |
320 |
|
|
NavigableMap s2 = null; |
321 |
|
|
try { |
322 |
|
|
s2 = (NavigableMap) (s.getClass().newInstance()); |
323 |
|
|
s2.putAll(s); |
324 |
|
|
} |
325 |
|
|
catch (Exception e) { e.printStackTrace(); return; } |
326 |
jsr166 |
1.5 |
timer.finish(); |
327 |
|
|
|
328 |
dl |
1.1 |
timer.start("Iter Equals ", size * 2); |
329 |
|
|
boolean eqt = s2.equals(s) && s.equals(s2); |
330 |
jsr166 |
1.9 |
reallyAssert(eqt); |
331 |
jsr166 |
1.5 |
timer.finish(); |
332 |
dl |
1.1 |
|
333 |
|
|
timer.start("Iter HashCode ", size * 2); |
334 |
|
|
int shc = s.hashCode(); |
335 |
|
|
int s2hc = s2.hashCode(); |
336 |
jsr166 |
1.9 |
reallyAssert(shc == s2hc); |
337 |
jsr166 |
1.5 |
timer.finish(); |
338 |
dl |
1.1 |
|
339 |
|
|
timer.start("Put (present) ", size); |
340 |
|
|
s2.putAll(s); |
341 |
jsr166 |
1.5 |
timer.finish(); |
342 |
dl |
1.1 |
|
343 |
|
|
timer.start("Iter EntrySet contains ", size * 2); |
344 |
|
|
Set es2 = s2.entrySet(); |
345 |
|
|
int sum = 0; |
346 |
|
|
for (Iterator i1 = s.entrySet().iterator(); i1.hasNext(); ) { |
347 |
|
|
Object entry = i1.next(); |
348 |
|
|
if (es2.contains(entry)) ++sum; |
349 |
|
|
} |
350 |
jsr166 |
1.5 |
timer.finish(); |
351 |
jsr166 |
1.9 |
reallyAssert(sum == size); |
352 |
dl |
1.1 |
|
353 |
|
|
t6("Get ", size, s2, key, absent); |
354 |
|
|
|
355 |
|
|
Object hold = s2.get(key[size-1]); |
356 |
|
|
s2.put(key[size-1], absent[0]); |
357 |
|
|
timer.start("Iter Equals ", size * 2); |
358 |
|
|
eqt = s2.equals(s) && s.equals(s2); |
359 |
jsr166 |
1.9 |
reallyAssert(!eqt); |
360 |
jsr166 |
1.5 |
timer.finish(); |
361 |
dl |
1.1 |
|
362 |
|
|
timer.start("Iter HashCode ", size * 2); |
363 |
|
|
int s1h = s.hashCode(); |
364 |
|
|
int s2h = s2.hashCode(); |
365 |
jsr166 |
1.9 |
reallyAssert(s1h != s2h); |
366 |
jsr166 |
1.5 |
timer.finish(); |
367 |
dl |
1.1 |
|
368 |
|
|
s2.put(key[size-1], hold); |
369 |
|
|
timer.start("Remove (iterator) ", size * 2); |
370 |
|
|
Iterator s2i = s2.entrySet().iterator(); |
371 |
|
|
Set es = s.entrySet(); |
372 |
|
|
while (s2i.hasNext()) |
373 |
|
|
es.remove(s2i.next()); |
374 |
jsr166 |
1.5 |
timer.finish(); |
375 |
dl |
1.1 |
|
376 |
jsr166 |
1.9 |
reallyAssert(s.isEmpty()); |
377 |
dl |
1.1 |
|
378 |
|
|
timer.start("Clear ", size); |
379 |
|
|
s2.clear(); |
380 |
jsr166 |
1.5 |
timer.finish(); |
381 |
jsr166 |
1.9 |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
382 |
dl |
1.1 |
} |
383 |
|
|
|
384 |
|
|
static void test(NavigableMap s, Integer[] key) { |
385 |
|
|
int size = key.length; |
386 |
|
|
|
387 |
|
|
t3("Put (absent) ", size, s, key, size); |
388 |
|
|
t3("Put (present) ", size, s, key, 0); |
389 |
|
|
t7("ContainsKey ", size, s, key, absent); |
390 |
|
|
t4("ContainsKey ", size, s, key, size); |
391 |
|
|
ktest(s, size, key); |
392 |
|
|
t4("ContainsKey ", absentSize, s, absent, 0); |
393 |
|
|
t6("Get ", size, s, key, absent); |
394 |
|
|
t1("Get (present) ", size, s, key, size); |
395 |
|
|
t1("Get (absent) ", absentSize, s, absent, 0); |
396 |
|
|
t2("Remove (absent) ", absentSize, s, absent, 0); |
397 |
|
|
t5("Remove (present) ", size, s, key, size / 2); |
398 |
|
|
t3("Put (half present) ", size, s, key, size / 2); |
399 |
|
|
|
400 |
|
|
ittest(s, size); |
401 |
|
|
rittest(s, size); |
402 |
|
|
higherTest(s); |
403 |
|
|
ceilingTest(s); |
404 |
|
|
floorTest(s); |
405 |
|
|
lowerTest(s); |
406 |
|
|
t9(s); |
407 |
|
|
rtest(s, size); |
408 |
|
|
|
409 |
|
|
t4("ContainsKey ", size, s, key, 0); |
410 |
|
|
t2("Remove (absent) ", size, s, key, 0); |
411 |
|
|
t3("Put (presized) ", size, s, key, size); |
412 |
|
|
dtest(s, size, key); |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
static class TestTimer { |
416 |
|
|
private String name; |
417 |
|
|
private long numOps; |
418 |
|
|
private long startTime; |
419 |
|
|
private String cname; |
420 |
|
|
|
421 |
|
|
static final java.util.TreeMap accum = new java.util.TreeMap(); |
422 |
jsr166 |
1.5 |
|
423 |
dl |
1.1 |
static void printStats() { |
424 |
|
|
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
425 |
jsr166 |
1.7 |
Map.Entry e = (Map.Entry) it.next(); |
426 |
|
|
Stats stats = (Stats) e.getValue(); |
427 |
dl |
1.1 |
int n = stats.number; |
428 |
|
|
double t; |
429 |
jsr166 |
1.5 |
if (n > 0) |
430 |
dl |
1.1 |
t = stats.sum / n; |
431 |
|
|
else |
432 |
|
|
t = stats.least; |
433 |
|
|
long nano = Math.round(1000000.0 * t); |
434 |
|
|
System.out.println(e.getKey() + ": " + nano); |
435 |
|
|
} |
436 |
|
|
} |
437 |
jsr166 |
1.5 |
|
438 |
dl |
1.1 |
void start(String name, long numOps) { |
439 |
|
|
this.name = name; |
440 |
|
|
this.cname = classify(); |
441 |
|
|
this.numOps = numOps; |
442 |
|
|
startTime = System.currentTimeMillis(); |
443 |
|
|
} |
444 |
jsr166 |
1.5 |
|
445 |
dl |
1.1 |
String classify() { |
446 |
|
|
if (name.startsWith("Get")) |
447 |
|
|
return "Get "; |
448 |
|
|
else if (name.startsWith("Put")) |
449 |
|
|
return "Put "; |
450 |
|
|
else if (name.startsWith("Remove")) |
451 |
|
|
return "Remove "; |
452 |
|
|
else if (name.startsWith("Iter")) |
453 |
|
|
return "Iter "; |
454 |
jsr166 |
1.5 |
else |
455 |
dl |
1.1 |
return null; |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
void finish() { |
459 |
|
|
long endTime = System.currentTimeMillis(); |
460 |
|
|
long time = endTime - startTime; |
461 |
jsr166 |
1.8 |
double timePerOp = (double) time /numOps; |
462 |
dl |
1.1 |
|
463 |
|
|
Object st = accum.get(name); |
464 |
|
|
if (st == null) |
465 |
|
|
accum.put(name, new Stats(timePerOp)); |
466 |
|
|
else { |
467 |
|
|
Stats stats = (Stats) st; |
468 |
|
|
stats.sum += timePerOp; |
469 |
|
|
stats.number++; |
470 |
|
|
if (timePerOp < stats.least) stats.least = timePerOp; |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
if (cname != null) { |
474 |
|
|
st = accum.get(cname); |
475 |
|
|
if (st == null) |
476 |
|
|
accum.put(cname, new Stats(timePerOp)); |
477 |
|
|
else { |
478 |
|
|
Stats stats = (Stats) st; |
479 |
|
|
stats.sum += timePerOp; |
480 |
|
|
stats.number++; |
481 |
|
|
if (timePerOp < stats.least) stats.least = timePerOp; |
482 |
|
|
} |
483 |
|
|
} |
484 |
|
|
|
485 |
|
|
} |
486 |
|
|
|
487 |
|
|
} |
488 |
|
|
|
489 |
|
|
static class Stats { |
490 |
|
|
double sum = 0; |
491 |
|
|
double least; |
492 |
|
|
int number = 0; |
493 |
|
|
Stats(double t) { least = t; } |
494 |
|
|
} |
495 |
|
|
|
496 |
dl |
1.4 |
static Random rng = new Random(111); |
497 |
dl |
1.1 |
|
498 |
|
|
static void shuffle(Integer[] keys) { |
499 |
|
|
int size = keys.length; |
500 |
jsr166 |
1.11 |
for (int i = size; i > 1; i--) { |
501 |
dl |
1.1 |
int r = rng.nextInt(i); |
502 |
|
|
Integer t = keys[i-1]; |
503 |
|
|
keys[i-1] = keys[r]; |
504 |
|
|
keys[r] = t; |
505 |
|
|
} |
506 |
|
|
} |
507 |
|
|
|
508 |
|
|
} |