ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/IteratorLoops.java
Revision: 1.1
Committed: Mon Nov 28 15:40:56 2005 UTC (18 years, 5 months ago) by dl
Branch: MAIN
Log Message:
Update and add misc tests

File Contents

# Content
1 /*
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 * http://creativecommons.org/licenses/publicdomain
5 */
6
7 import java.util.*;
8
9 /**
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
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;
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;
24
25 public static void main(String[] args) throws Exception {
26 Class klass = Class.forName(args[0]);
27 int n = (args.length <= 1)? DEFAULT_SIZE : Integer.parseInt(args[1]);
28 int t = (args.length <= 2)? DEFAULT_TRIALS : Integer.parseInt(args[2]);
29
30 System.out.print("Class: " + klass.getName());
31 System.out.print(" ~iters: " + (long)n * (long)n);
32 System.out.print(" trials: " + t);
33 System.out.println();
34
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();
40
41 for (int i = 0; i < t; ++i)
42 new IteratorLoops(colls).oneRun(n);
43
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;
53 elementCount = 0;
54 }
55
56 void oneRun(int n) {
57 preload(n);
58 long startTime = System.nanoTime();
59 long count = traversals(n);
60 double elapsed = (double)(System.nanoTime() - startTime);
61 double npi = elapsed / count;
62 double secs = elapsed / 1000000000;
63 System.out.printf("%7.1f ns/iter %8.3fs run time\n", npi, secs);
64 }
65
66 long traversals(int n) {
67 long count = 0;
68 long check = 0;
69 for (int i = 0; i < n; i++) {
70 check += elementCount;
71 count += counts();
72 maybeAdd();
73 }
74 if (count != check)
75 mismatches = count;
76 return count;
77 }
78
79 int counts() {
80 int count = 0;
81 for (int k = 0; k < cs.length; ++k) {
82 for (Iterator it = cs[k].iterator(); it.hasNext();) {
83 if (it.next() != null)
84 ++count;
85 }
86 }
87 return count;
88 }
89
90 void maybeAdd() {
91 int r = randomSeed;
92 r ^= r << 6;
93 r ^= r >>> 21;
94 r ^= r << 7;
95 randomSeed = r;
96 if ((r >>> 29) == 0)
97 cs[r & (cs.length-1)].add(new Integer(elementCount++));
98 }
99
100 void preload(int n) {
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)
108 al.add(new Integer(elementCount++));
109 cs[i].addAll(al);
110 al.clear();
111 }
112 k >>>= 1;
113 }
114 // let GC settle down
115 try { Thread.sleep(500); } catch(Exception ex) { return; }
116 }
117
118
119 }