10 |
|
* Estimates time per iteration of collection iterators. Preloads |
11 |
|
* most elements, but adds about 1/8 of them dynamically to preclude |
12 |
|
* overly clever optimizations. The array of collections has |
13 |
< |
* approximately exponentially different lengths, so chek both short |
13 |
> |
* approximately exponentially different lengths, so check both short |
14 |
|
* and long iterators. Reports include times for adds and other |
15 |
|
* checks, so overestimate times per iteration. |
16 |
|
*/ |
17 |
|
|
18 |
|
public final class IteratorLoops { |
19 |
|
static final int DEFAULT_SIZE = 16384; |
20 |
< |
static final int DEFAULT_TRIALS = 2; |
20 |
> |
static final int DEFAULT_TRIALS = 4; |
21 |
|
static final int NC = 16; // number of collections must be power of 2 |
22 |
|
static volatile long mismatches = 0; |
23 |
|
static int randomSeed = 3122688; |
35 |
|
Collection<Integer>[] colls = |
36 |
|
(Collection<Integer>[])new Collection[NC]; |
37 |
|
|
38 |
< |
for (int k = 0; k < colls.length; ++k) |
39 |
< |
colls[k] = (Collection<Integer>)klass.newInstance(); |
38 |
> |
for (int k = 0; k < colls.length; ++k) { |
39 |
> |
Object x = klass.newInstance(); |
40 |
> |
if (x instanceof Collection) |
41 |
> |
colls[k] = (Collection<Integer>)x; |
42 |
> |
else if (x instanceof Map) |
43 |
> |
colls[k] = (Collection<Integer>)Collections.newSetFromMap((Map)x); |
44 |
> |
else |
45 |
> |
throw new Error("bad class"); |
46 |
> |
} |
47 |
|
|
48 |
< |
for (int i = 0; i < t; ++i) |
48 |
> |
for (int i = 0; i < t; ++i) |
49 |
|
new IteratorLoops(colls).oneRun(n); |
50 |
|
|
51 |
< |
if (mismatches != 0) |
51 |
> |
if (mismatches != 0) |
52 |
|
throw new Error("Bad checksum :" + mismatches); |
53 |
|
} |
54 |
|
|
55 |
|
private int elementCount; |
56 |
|
private final Collection<Integer>[] cs; |
57 |
|
|
58 |
< |
IteratorLoops(Collection<Integer>[] colls) { |
59 |
< |
cs = colls; |
58 |
> |
IteratorLoops(Collection<Integer>[] colls) { |
59 |
> |
cs = colls; |
60 |
|
elementCount = 0; |
61 |
|
} |
62 |
|
|
92 |
|
} |
93 |
|
} |
94 |
|
return count; |
95 |
< |
} |
95 |
> |
} |
96 |
|
|
97 |
|
void maybeAdd() { |
98 |
|
int r = randomSeed; |
99 |
< |
r ^= r << 6; |
100 |
< |
r ^= r >>> 21; |
99 |
> |
r ^= r << 6; |
100 |
> |
r ^= r >>> 21; |
101 |
|
r ^= r << 7; |
102 |
|
randomSeed = r; |
103 |
< |
if ((r >>> 29) == 0) |
103 |
> |
if ((r >>> 29) == 0) |
104 |
|
cs[r & (cs.length-1)].add(new Integer(elementCount++)); |
105 |
< |
} |
105 |
> |
} |
106 |
|
|
107 |
|
void preload(int n) { |
108 |
< |
for (int i = 0; i < cs.length; ++i) |
108 |
> |
for (int i = 0; i < cs.length; ++i) |
109 |
|
cs[i].clear(); |
110 |
|
int k = (n - n / 8) / 2; |
111 |
|
ArrayList<Integer> al = new ArrayList<Integer>(k+1); |
112 |
|
for (int i = 0; i < cs.length; ++i) { |
113 |
|
if (k > 0) { |
114 |
< |
for (int j = 0; j < k; ++j) |
114 |
> |
for (int j = 0; j < k; ++j) |
115 |
|
al.add(new Integer(elementCount++)); |
116 |
|
cs[i].addAll(al); |
117 |
|
al.clear(); |
120 |
|
} |
121 |
|
// let GC settle down |
122 |
|
try { Thread.sleep(500); } catch(Exception ex) { return; } |
123 |
< |
} |
123 |
> |
} |
124 |
|
|
125 |
|
|
126 |
|
} |