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.1 by dl, Tue Oct 4 20:09:41 2005 UTC vs.
Revision 1.12 by jsr166, Sat Dec 31 19:18:26 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. Use, modify, and
4 < * redistribute this code in any way without acknowledgement.
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 <
8 < import java.util.*;
9 < import java.util.concurrent.*;
7 > import java.util.Collection;
8 > import java.util.Random;
9 > import java.util.concurrent.CyclicBarrier;
10 > import java.util.concurrent.ExecutorService;
11 > import java.util.concurrent.Executors;
12  
13   public class CollectionLoops {
14      static int pinsert     = 100;
# Line 23 | Line 25 | public class CollectionLoops {
25          int nkeys       = 10000;
26          int nops        = 100000;
27  
28 <        Class collectionClass = null;
28 >        Class<?> collectionClass = null;
29          if (args.length > 0) {
30              try {
31                  collectionClass = Class.forName(args[0]);
32 <            } catch(ClassNotFoundException e) {
32 >            } catch (ClassNotFoundException e) {
33                  throw new RuntimeException("Class " + args[0] + " not found.");
34              }
35          }
36  
37 <        if (args.length > 1)
37 >        if (args.length > 1)
38              maxThreads = Integer.parseInt(args[1]);
39  
40 <        if (args.length > 2)
40 >        if (args.length > 2)
41              nkeys = Integer.parseInt(args[2]);
42  
43 <        if (args.length > 3)
43 >        if (args.length > 3)
44              pinsert = Integer.parseInt(args[3]);
45  
46 <        if (args.length > 4)
46 >        if (args.length > 4)
47              premove = Integer.parseInt(args[4]);
48  
49 <        if (args.length > 5)
49 >        if (args.length > 5)
50              nops = Integer.parseInt(args[5]);
51  
52          // normalize probabilities wrt random number generator
53          removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL));
54          insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL));
55 <        
55 >
56          System.out.print("Class: " + collectionClass.getName());
57          System.out.print(" threads: " + maxThreads);
58          System.out.print(" size: " + nkeys);
# Line 65 | Line 67 | public class CollectionLoops {
67          test(4, 100, 100, collectionClass);
68          print = true;
69  
68        int k = 1;
70          int warmups = 2;
71 <        for (int i = 1; i <= maxThreads;) {
71 >        for (int k = 1, i = 1; i <= maxThreads;) {
72              Thread.sleep(100);
73              test(i, nkeys, nops, collectionClass);
74              if (warmups > 0)
# Line 75 | Line 76 | public class CollectionLoops {
76              else if (i == k) {
77                  k = i << 1;
78                  i = i + (i >>> 1);
79 <            }
79 >            }
80              else if (i == 1 && k == 2) {
81                  i = k;
82                  warmups = 1;
83              }
84 <            else
84 >            else
85                  i = k;
86          }
87          pool.shutdown();
# Line 89 | Line 90 | public class CollectionLoops {
90      static Integer[] makeKeys(int n) {
91          LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
92          Integer[] key = new Integer[n];
93 <        for (int i = 0; i < key.length; ++i)
93 >        for (int i = 0; i < key.length; ++i)
94              key[i] = new Integer(rng.next());
95          return key;
96      }
# Line 104 | Line 105 | public class CollectionLoops {
105          }
106      }
107  
108 <    static void test(int i, int nk, int nops, Class collectionClass) throws Exception {
108 >    static void test(int i, int nk, int nops, Class<?> collectionClass) throws Exception {
109          if (print)
110              System.out.print("Threads: " + i + "\t:");
111 <        Collection<Integer> collection = (Collection<Integer>)collectionClass.newInstance();
111 >        Collection<Integer> collection =
112 >            (Collection<Integer>) collectionClass.getConstructor().newInstance();
113          Integer[] key = makeKeys(nk);
114          // Uncomment to start with a non-empty table
115 <        for (int j = 0; j < nk; j += 2)
115 >        for (int j = 0; j < nk; j += 2)
116              collection.add(key[j]);
117          shuffleKeys(key);
118          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
119          CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
120 <        for (int t = 0; t < i; ++t)
120 >        for (int t = 0; t < i; ++t)
121              pool.execute(new Runner(t, collection, key, barrier, nops));
122          barrier.await();
123          barrier.await();
124          long time = timer.getTime();
125 <        long tpo = time / (i * (long)nops);
125 >        long tpo = time / (i * (long) nops);
126          if (print)
127              System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
128 <        double secs = (double)(time) / 1000000000.0;
128 >        double secs = (double) time / 1000000000.0;
129          if (print)
130              System.out.print("\t " + secs + "s run time");
131          if (checkSum == 0) System.out.print(" ");
# Line 141 | Line 143 | public class CollectionLoops {
143          int total;
144          int nops;
145  
146 <        Runner(int id, Collection<Integer> collection, Integer[] key,  CyclicBarrier barrier, int nops) {
147 <            this.collection = collection;
148 <            this.key = key;
146 >        Runner(int id, Collection<Integer> collection, Integer[] key, CyclicBarrier barrier, int nops) {
147 >            this.collection = collection;
148 >            this.key = key;
149              this.barrier = barrier;
150              this.nops = nops;
151 <            position = key.length / (id + 1);
151 >            position = key.length / (id + 1);
152              rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
153              rng.next();
154          }
# Line 160 | Line 162 | public class CollectionLoops {
162                  while (ops > 0) {
163                      int r = rng.next();
164                      p += (r & 7) - 3;
165 <                    while (p >= key.length) p -= key.length;  
165 >                    while (p >= key.length) p -= key.length;
166                      while (p < 0) p += key.length;
167  
168                      Integer k = key[p];
169                      if (c.contains(k)) {
170                          if (r < removesPerMaxRandom) {
171                              if (c.remove(k)) {
172 <                                p = Math.abs(total % key.length);
172 >                                p = Math.abs(total % key.length);
173                                  ops -= 2;
174                                  continue;
175                              }
# Line 178 | Line 180 | public class CollectionLoops {
180                          ops -= 2;
181                          c.add(k);
182                          continue;
183 <                    }
184 <                    
183 >                    }
184 >
185                      total += LoopHelpers.compute6(k.intValue());
186                      --ops;
187                  }
# Line 192 | Line 194 | public class CollectionLoops {
194          }
195      }
196   }
195

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines