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) |
38 |
> |
for (int k = 0; k < colls.length; ++k) |
39 |
|
colls[k] = (Collection<Integer>)klass.newInstance(); |
40 |
|
|
41 |
< |
for (int i = 0; i < t; ++i) |
41 |
> |
for (int i = 0; i < t; ++i) |
42 |
|
new IteratorLoops(colls).oneRun(n); |
43 |
|
|
44 |
< |
if (mismatches != 0) |
44 |
> |
if (mismatches != 0) |
45 |
|
throw new Error("Bad checksum :" + mismatches); |
46 |
|
} |
47 |
|
|
48 |
|
private int elementCount; |
49 |
|
private final Collection<Integer>[] cs; |
50 |
|
|
51 |
< |
IteratorLoops(Collection<Integer>[] colls) { |
52 |
< |
cs = colls; |
51 |
> |
IteratorLoops(Collection<Integer>[] colls) { |
52 |
> |
cs = colls; |
53 |
|
elementCount = 0; |
54 |
|
} |
55 |
|
|
85 |
|
} |
86 |
|
} |
87 |
|
return count; |
88 |
< |
} |
88 |
> |
} |
89 |
|
|
90 |
|
void maybeAdd() { |
91 |
|
int r = randomSeed; |
92 |
< |
r ^= r << 6; |
93 |
< |
r ^= r >>> 21; |
92 |
> |
r ^= r << 6; |
93 |
> |
r ^= r >>> 21; |
94 |
|
r ^= r << 7; |
95 |
|
randomSeed = r; |
96 |
< |
if ((r >>> 29) == 0) |
96 |
> |
if ((r >>> 29) == 0) |
97 |
|
cs[r & (cs.length-1)].add(new Integer(elementCount++)); |
98 |
< |
} |
98 |
> |
} |
99 |
|
|
100 |
|
void preload(int n) { |
101 |
< |
for (int i = 0; i < cs.length; ++i) |
101 |
> |
for (int i = 0; i < cs.length; ++i) |
102 |
|
cs[i].clear(); |
103 |
|
int k = (n - n / 8) / 2; |
104 |
|
ArrayList<Integer> al = new ArrayList<Integer>(k+1); |
105 |
|
for (int i = 0; i < cs.length; ++i) { |
106 |
|
if (k > 0) { |
107 |
< |
for (int j = 0; j < k; ++j) |
107 |
> |
for (int j = 0; j < k; ++j) |
108 |
|
al.add(new Integer(elementCount++)); |
109 |
|
cs[i].addAll(al); |
110 |
|
al.clear(); |
113 |
|
} |
114 |
|
// let GC settle down |
115 |
|
try { Thread.sleep(500); } catch(Exception ex) { return; } |
116 |
< |
} |
116 |
> |
} |
117 |
|
|
118 |
|
|
119 |
|
} |