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

Comparing jsr166/src/test/loops/CollectionLoops.java (file contents):
Revision 1.2 by dl, Mon Feb 19 00:46:06 2007 UTC vs.
Revision 1.11 by jsr166, Sun Oct 23 03:03:23 2016 UTC

# Line 1 | Line 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 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7
7   import java.util.*;
8   import java.util.concurrent.*;
9  
# Line 23 | Line 22 | public class CollectionLoops {
22          int nkeys       = 10000;
23          int nops        = 100000;
24  
25 <        Class collectionClass = null;
25 >        Class<?> collectionClass = null;
26          if (args.length > 0) {
27              try {
28                  collectionClass = Class.forName(args[0]);
29 <            } catch(ClassNotFoundException e) {
29 >            } catch (ClassNotFoundException e) {
30                  throw new RuntimeException("Class " + args[0] + " not found.");
31              }
32          }
33  
34 <        if (args.length > 1)
34 >        if (args.length > 1)
35              maxThreads = Integer.parseInt(args[1]);
36  
37 <        if (args.length > 2)
37 >        if (args.length > 2)
38              nkeys = Integer.parseInt(args[2]);
39  
40 <        if (args.length > 3)
40 >        if (args.length > 3)
41              pinsert = Integer.parseInt(args[3]);
42  
43 <        if (args.length > 4)
43 >        if (args.length > 4)
44              premove = Integer.parseInt(args[4]);
45  
46 <        if (args.length > 5)
46 >        if (args.length > 5)
47              nops = Integer.parseInt(args[5]);
48  
49          // normalize probabilities wrt random number generator
50          removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL));
51          insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL));
52 <        
52 >
53          System.out.print("Class: " + collectionClass.getName());
54          System.out.print(" threads: " + maxThreads);
55          System.out.print(" size: " + nkeys);
# Line 65 | Line 64 | public class CollectionLoops {
64          test(4, 100, 100, collectionClass);
65          print = true;
66  
68        int k = 1;
67          int warmups = 2;
68 <        for (int i = 1; i <= maxThreads;) {
68 >        for (int k = 1, i = 1; i <= maxThreads;) {
69              Thread.sleep(100);
70              test(i, nkeys, nops, collectionClass);
71              if (warmups > 0)
# Line 75 | Line 73 | public class CollectionLoops {
73              else if (i == k) {
74                  k = i << 1;
75                  i = i + (i >>> 1);
76 <            }
76 >            }
77              else if (i == 1 && k == 2) {
78                  i = k;
79                  warmups = 1;
80              }
81 <            else
81 >            else
82                  i = k;
83          }
84          pool.shutdown();
# Line 89 | Line 87 | public class CollectionLoops {
87      static Integer[] makeKeys(int n) {
88          LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
89          Integer[] key = new Integer[n];
90 <        for (int i = 0; i < key.length; ++i)
90 >        for (int i = 0; i < key.length; ++i)
91              key[i] = new Integer(rng.next());
92          return key;
93      }
# Line 104 | Line 102 | public class CollectionLoops {
102          }
103      }
104  
105 <    static void test(int i, int nk, int nops, Class collectionClass) throws Exception {
105 >    static void test(int i, int nk, int nops, Class<?> collectionClass) throws Exception {
106          if (print)
107              System.out.print("Threads: " + i + "\t:");
108 <        Collection<Integer> collection = (Collection<Integer>)collectionClass.newInstance();
108 >        Collection<Integer> collection =
109 >            (Collection<Integer>) collectionClass.getConstructor().newInstance();
110          Integer[] key = makeKeys(nk);
111          // Uncomment to start with a non-empty table
112 <        for (int j = 0; j < nk; j += 2)
112 >        for (int j = 0; j < nk; j += 2)
113              collection.add(key[j]);
114          shuffleKeys(key);
115          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
116          CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
117 <        for (int t = 0; t < i; ++t)
117 >        for (int t = 0; t < i; ++t)
118              pool.execute(new Runner(t, collection, key, barrier, nops));
119          barrier.await();
120          barrier.await();
121          long time = timer.getTime();
122 <        long tpo = time / (i * (long)nops);
122 >        long tpo = time / (i * (long) nops);
123          if (print)
124              System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
125 <        double secs = (double)(time) / 1000000000.0;
125 >        double secs = (double) time / 1000000000.0;
126          if (print)
127              System.out.print("\t " + secs + "s run time");
128          if (checkSum == 0) System.out.print(" ");
# Line 141 | Line 140 | public class CollectionLoops {
140          int total;
141          int nops;
142  
143 <        Runner(int id, Collection<Integer> collection, Integer[] key,  CyclicBarrier barrier, int nops) {
144 <            this.collection = collection;
145 <            this.key = key;
143 >        Runner(int id, Collection<Integer> collection, Integer[] key, CyclicBarrier barrier, int nops) {
144 >            this.collection = collection;
145 >            this.key = key;
146              this.barrier = barrier;
147              this.nops = nops;
148 <            position = key.length / (id + 1);
148 >            position = key.length / (id + 1);
149              rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
150              rng.next();
151          }
# Line 160 | Line 159 | public class CollectionLoops {
159                  while (ops > 0) {
160                      int r = rng.next();
161                      p += (r & 7) - 3;
162 <                    while (p >= key.length) p -= key.length;  
162 >                    while (p >= key.length) p -= key.length;
163                      while (p < 0) p += key.length;
164  
165                      Integer k = key[p];
166                      if (c.contains(k)) {
167                          if (r < removesPerMaxRandom) {
168                              if (c.remove(k)) {
169 <                                p = Math.abs(total % key.length);
169 >                                p = Math.abs(total % key.length);
170                                  ops -= 2;
171                                  continue;
172                              }
# Line 178 | Line 177 | public class CollectionLoops {
177                          ops -= 2;
178                          c.add(k);
179                          continue;
180 <                    }
181 <                    
180 >                    }
181 >
182                      total += LoopHelpers.compute6(k.intValue());
183                      --ops;
184                  }
# Line 192 | Line 191 | public class CollectionLoops {
191          }
192      }
193   }
195

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines