ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/IteratorLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/IteratorLoops.java (file contents):
Revision 1.1 by dl, Mon Nov 28 15:40:56 2005 UTC vs.
Revision 1.7 by jsr166, Tue Nov 3 01:04:02 2009 UTC

# Line 10 | Line 10 | import java.util.*;
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;
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]);
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);
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();
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>)
44 >                    Collections.newSetFromMap((Map) x);
45 >            else
46 >                throw new Error("bad class");
47 >        }
48  
49 <        for (int i = 0; i < t; ++i)
49 >        for (int i = 0; i < t; ++i)
50              new IteratorLoops(colls).oneRun(n);
51  
52 <        if (mismatches != 0)
52 >        if (mismatches != 0)
53              throw new Error("Bad checksum :" + mismatches);
54      }
55  
56      private int elementCount;
57      private final Collection<Integer>[] cs;
58  
59 <    IteratorLoops(Collection<Integer>[] colls) {
60 <        cs = colls;
59 >    IteratorLoops(Collection<Integer>[] colls) {
60 >        cs = colls;
61          elementCount = 0;
62      }
63  
# Line 57 | Line 65 | public final class IteratorLoops {
65          preload(n);
66          long startTime = System.nanoTime();
67          long count = traversals(n);
68 <        double elapsed = (double)(System.nanoTime() - startTime);
68 >        double elapsed = (double) (System.nanoTime() - startTime);
69          double npi = elapsed / count;
70          double secs = elapsed / 1000000000;
71          System.out.printf("%7.1f ns/iter  %8.3fs run time\n", npi, secs);
# Line 85 | Line 93 | public final class IteratorLoops {
93              }
94          }
95          return count;
96 <    }    
96 >    }
97  
98      void maybeAdd() {
99          int r = randomSeed;
100 <        r ^= r << 6;
101 <        r ^= r >>> 21;
100 >        r ^= r << 6;
101 >        r ^= r >>> 21;
102          r ^= r << 7;
103          randomSeed = r;
104 <        if ((r >>> 29) == 0)
104 >        if ((r >>> 29) == 0)
105              cs[r & (cs.length-1)].add(new Integer(elementCount++));
106 <    }    
106 >    }
107  
108      void preload(int n) {
109 <        for (int i = 0; i < cs.length; ++i)
109 >        for (int i = 0; i < cs.length; ++i)
110              cs[i].clear();
111          int k = (n - n / 8) / 2;
112          ArrayList<Integer> al = new ArrayList<Integer>(k+1);
113          for (int i = 0; i < cs.length; ++i) {
114              if (k > 0) {
115 <                for (int j = 0; j < k; ++j)
115 >                for (int j = 0; j < k; ++j)
116                      al.add(new Integer(elementCount++));
117                  cs[i].addAll(al);
118                  al.clear();
# Line 112 | Line 120 | public final class IteratorLoops {
120              k >>>= 1;
121          }
122          // let GC settle down
123 <        try { Thread.sleep(500); } catch(Exception ex) { return; }
124 <    }    
123 >        try { Thread.sleep(500); }
124 >        catch (Exception ex) { return; }
125 >    }
126  
127  
128   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines