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

Comparing jsr166/src/test/loops/CollectionWordLoops.java (file contents):
Revision 1.1 by dl, Tue May 31 15:08:32 2005 UTC vs.
Revision 1.10 by jsr166, Thu Jan 15 18:34:18 2015 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 import java.util.*;
6   import java.io.*;
7 + import java.util.*;
8  
9   public class CollectionWordLoops {
10  
11      static final String[] WORDS_FILES = {
12 <        "kw.txt",
12 >        "kw.txt",
13          "class.txt",
14 <        "dir.txt",
15 <        "ids.txt",
16 <        "/usr/dict/words",
14 >        //        "dir.txt",
15 >        //        "ids.txt",
16 >        //        "/usr/dict/words",
17      };
18  
19      static final int MAX_WORDS = 500000;
# Line 23 | Line 23 | public class CollectionWordLoops {
23      static final int numTests  = 2;
24  
25      public static void main(String[] args) {
26 <        Class collectionClass = null;
26 >        Class<?> collectionClass = null;
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          System.out.println("Testing " + collectionClass.getName());
34  
35 <        for (int s = 0; s < WORDS_FILES.length; ++s)
35 >        for (int s = 0; s < WORDS_FILES.length; ++s)
36              tests(collectionClass, numTests, s);
37  
38 <        for (int s = WORDS_FILES.length-1; s >= 0; --s)
38 >        for (int s = WORDS_FILES.length-1; s >= 0; --s)
39              tests(collectionClass, numTests, s);
40  
41      }
42  
43 <    static void tests(Class collectionClass, int numTests, int sizeIndex) {
44 <        try {
43 >    static void tests(Class<?> collectionClass, int numTests, int sizeIndex) {
44 >        try {
45              String[] key = readWords(sizeIndex);
46              int size = key.length;
47 <            
47 >
48              System.out.print("n = " +LoopHelpers.rightJustify(size) +" : ");
49              long least = Long.MAX_VALUE;
50 <            
50 >
51              for (int i = 0; i < numTests; ++i) {
52                  Collection<String> m = newCollection(collectionClass);
53                  long t = doTest(collectionClass.getName(), m, key);
# Line 55 | Line 55 | public class CollectionWordLoops {
55                  m.clear();
56                  m = null;
57              }
58 <            
58 >
59              long nano = Math.round(1000000.0 * (least) / NOPS);
60              System.out.println(LoopHelpers.rightJustify(nano) + " ns per op");
61          } catch (IOException ignore) {
# Line 63 | Line 63 | public class CollectionWordLoops {
63          }
64      }
65  
66 <
67 <    static Collection<String> newCollection(Class cl) {
66 >    static Collection<String> newCollection(Class<?> cl) {
67          try {
68 <            Collection m = (Collection<String>)cl.newInstance();
68 >            Collection m = (Collection<String>) cl.newInstance();
69              return m;
70 <        } catch(Exception e) {
70 >        } catch (Exception e) {
71              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
72          }
73      }
74  
75      static void pause() {
76 <        try { Thread.sleep(100); } catch(InterruptedException ie) { return; }
76 >        try { Thread.sleep(100); }
77 >        catch (InterruptedException ie) { return; }
78      }
79  
80      static String[] readWords(int sizeIndex) throws IOException {
# Line 105 | Line 105 | public class CollectionWordLoops {
105      }
106  
107      static long doTest(String name,
108 <                       final Collection<String> m,
108 >                       final Collection<String> m,
109                         final String[] key) {
110  
111          //    System.out.print(name + "\t");
# Line 113 | Line 113 | public class CollectionWordLoops {
113          long startTime = System.currentTimeMillis();
114          runner.run();
115          long afterRun = System.currentTimeMillis();
116 <        long runTime =  (afterRun - startTime);
116 >        long runTime = afterRun - startTime;
117          int np = runner.total;
118 <        if (runner.total == runner.hashCode())
118 >        if (runner.total == runner.hashCode())
119              System.out.println("Useless Number" + runner.total);
120          int sz = runner.maxsz;
121 <        if (sz == runner.hashCode())
121 >        if (sz == runner.hashCode())
122              System.out.println("Useless Number" + sz);
123          //        System.out.print(" m = " + sz);
124          return runTime;
125      }
126  
127
127      static class Runner implements Runnable {
128          final Collection<String> collection;
129          final String[] key;
# Line 139 | Line 138 | public class CollectionWordLoops {
138          int maxsz;
139  
140          Runner(Collection<String> m, String[] k) {
141 <            collection = m; key = k;
141 >            collection = m; key = k;
142              pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50);
143              pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50);
144          }
145  
147
146          int oneStep(int j) {
147              int n = key.length;
148              int r = rng.next() & 0x7FFFFFFF;
149              int jinc = (r & 7);
150              j += jinc - 3;
151 <            if (j >= n) j -= n;  
151 >            if (j >= n) j -= n;
152              if (j < 0) j += n;
153  
154              int l = n / 4 + j;
155 <            if (l >= n) l -= n;  
156 <      
155 >            if (l >= n) l -= n;
156 >
157              String k = key[j];
158 <      
158 >
159              if (!collection.contains(k)) {
160                  ++nagets;
161                  if (r < pctins) {
# Line 172 | Line 170 | public class CollectionWordLoops {
170                  if (r < pctrem) {
171                      collection.remove(k);
172                      ++nremoves;
173 <                    j += ((r >>> 8) & 7) +  n / 2;
173 >                    j += ((r >>> 8) & 7) + n / 2;
174                      if (j >= n) j -= n;
175                  }
176              }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines