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