ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/IteratorLoops.java
Revision: 1.10
Committed: Sun Oct 21 06:40:21 2012 UTC (11 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +0 -1 lines
Log Message:
no blank line between javadoc and corresponding code

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.9 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
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 jsr166 1.2 * approximately exponentially different lengths, so check both short
14 dl 1.1 * and long iterators. Reports include times for adds and other
15     * checks, so overestimate times per iteration.
16     */
17     public final class IteratorLoops {
18     static final int DEFAULT_SIZE = 16384;
19 dl 1.3 static final int DEFAULT_TRIALS = 4;
20 dl 1.1 static final int NC = 16; // number of collections must be power of 2
21     static volatile long mismatches = 0;
22     static int randomSeed = 3122688;
23    
24     public static void main(String[] args) throws Exception {
25     Class klass = Class.forName(args[0]);
26 jsr166 1.5 int n = (args.length <= 1) ? DEFAULT_SIZE : Integer.parseInt(args[1]);
27     int t = (args.length <= 2) ? DEFAULT_TRIALS : Integer.parseInt(args[2]);
28 dl 1.1
29     System.out.print("Class: " + klass.getName());
30 jsr166 1.7 System.out.print(" ~iters: " + (long) n * (long) n);
31 dl 1.1 System.out.print(" trials: " + t);
32     System.out.println();
33    
34     Collection<Integer>[] colls =
35     (Collection<Integer>[])new Collection[NC];
36    
37 dl 1.4 for (int k = 0; k < colls.length; ++k) {
38     Object x = klass.newInstance();
39     if (x instanceof Collection)
40 jsr166 1.6 colls[k] = (Collection<Integer>) x;
41 dl 1.4 else if (x instanceof Map)
42 jsr166 1.6 colls[k] = (Collection<Integer>)
43     Collections.newSetFromMap((Map) x);
44 dl 1.4 else
45     throw new Error("bad class");
46     }
47 dl 1.1
48 jsr166 1.2 for (int i = 0; i < t; ++i)
49 dl 1.1 new IteratorLoops(colls).oneRun(n);
50    
51 jsr166 1.2 if (mismatches != 0)
52 dl 1.1 throw new Error("Bad checksum :" + mismatches);
53     }
54    
55     private int elementCount;
56     private final Collection<Integer>[] cs;
57    
58 jsr166 1.2 IteratorLoops(Collection<Integer>[] colls) {
59     cs = colls;
60 dl 1.1 elementCount = 0;
61     }
62    
63     void oneRun(int n) {
64     preload(n);
65     long startTime = System.nanoTime();
66     long count = traversals(n);
67 jsr166 1.7 double elapsed = (double) (System.nanoTime() - startTime);
68 dl 1.1 double npi = elapsed / count;
69     double secs = elapsed / 1000000000;
70     System.out.printf("%7.1f ns/iter %8.3fs run time\n", npi, secs);
71     }
72    
73     long traversals(int n) {
74     long count = 0;
75     long check = 0;
76 jsr166 1.8 for (int i = 0; i < n; i++) {
77 dl 1.1 check += elementCount;
78     count += counts();
79     maybeAdd();
80     }
81     if (count != check)
82     mismatches = count;
83     return count;
84     }
85    
86     int counts() {
87     int count = 0;
88     for (int k = 0; k < cs.length; ++k) {
89     for (Iterator it = cs[k].iterator(); it.hasNext();) {
90     if (it.next() != null)
91     ++count;
92     }
93     }
94     return count;
95 jsr166 1.2 }
96 dl 1.1
97     void maybeAdd() {
98     int r = randomSeed;
99 jsr166 1.2 r ^= r << 6;
100     r ^= r >>> 21;
101 dl 1.1 r ^= r << 7;
102     randomSeed = r;
103 jsr166 1.2 if ((r >>> 29) == 0)
104 dl 1.1 cs[r & (cs.length-1)].add(new Integer(elementCount++));
105 jsr166 1.2 }
106 dl 1.1
107     void preload(int n) {
108 jsr166 1.2 for (int i = 0; i < cs.length; ++i)
109 dl 1.1 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 jsr166 1.2 for (int j = 0; j < k; ++j)
115 dl 1.1 al.add(new Integer(elementCount++));
116     cs[i].addAll(al);
117     al.clear();
118     }
119     k >>>= 1;
120     }
121     // let GC settle down
122 jsr166 1.5 try { Thread.sleep(500); }
123     catch (Exception ex) { return; }
124 jsr166 1.2 }
125 dl 1.1
126    
127     }