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.12 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 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
40      }
41  
42 <    static void tests(Class collectionClass, int numTests, int sizeIndex) {
43 <        try {
42 >    static void tests(Class<?> collectionClass, int numTests, int sizeIndex) {
43 >        try {
44              String[] key = readWords(sizeIndex);
45              int size = key.length;
46 <            
46 >
47              System.out.print("n = " +LoopHelpers.rightJustify(size) +" : ");
48              long least = Long.MAX_VALUE;
49 <            
49 >
50              for (int i = 0; i < numTests; ++i) {
51                  Collection<String> m = newCollection(collectionClass);
52                  long t = doTest(collectionClass.getName(), m, key);
# Line 55 | Line 54 | public class CollectionWordLoops {
54                  m.clear();
55                  m = null;
56              }
57 <            
57 >
58              long nano = Math.round(1000000.0 * (least) / NOPS);
59              System.out.println(LoopHelpers.rightJustify(nano) + " ns per op");
60          } catch (IOException ignore) {
# Line 63 | Line 62 | public class CollectionWordLoops {
62          }
63      }
64  
65 <
67 <    static Collection<String> newCollection(Class cl) {
65 >    static Collection<String> newCollection(Class<?> cl) {
66          try {
67 <            Collection m = (Collection<String>)cl.newInstance();
68 <            return m;
71 <        } catch(Exception e) {
67 >            return (Collection<String>) cl.getConstructor().newInstance();
68 >        } catch (Exception e) {
69              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
70          }
71      }
72  
73      static void pause() {
74 <        try { Thread.sleep(100); } catch(InterruptedException ie) { return; }
74 >        try { Thread.sleep(100); }
75 >        catch (InterruptedException ie) { return; }
76      }
77  
78      static String[] readWords(int sizeIndex) throws IOException {
# Line 105 | Line 103 | public class CollectionWordLoops {
103      }
104  
105      static long doTest(String name,
106 <                       final Collection<String> m,
106 >                       final Collection<String> m,
107                         final String[] key) {
108  
109          //    System.out.print(name + "\t");
# Line 113 | Line 111 | public class CollectionWordLoops {
111          long startTime = System.currentTimeMillis();
112          runner.run();
113          long afterRun = System.currentTimeMillis();
114 <        long runTime =  (afterRun - startTime);
114 >        long runTime = afterRun - startTime;
115          int np = runner.total;
116 <        if (runner.total == runner.hashCode())
116 >        if (runner.total == runner.hashCode())
117              System.out.println("Useless Number" + runner.total);
118          int sz = runner.maxsz;
119 <        if (sz == runner.hashCode())
119 >        if (sz == runner.hashCode())
120              System.out.println("Useless Number" + sz);
121          //        System.out.print(" m = " + sz);
122          return runTime;
123      }
124  
127
125      static class Runner implements Runnable {
126          final Collection<String> collection;
127          final String[] key;
# Line 139 | Line 136 | public class CollectionWordLoops {
136          int maxsz;
137  
138          Runner(Collection<String> m, String[] k) {
139 <            collection = m; key = k;
139 >            collection = m; key = k;
140              pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50);
141              pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50);
142          }
143  
147
144          int oneStep(int j) {
145              int n = key.length;
146              int r = rng.next() & 0x7FFFFFFF;
147              int jinc = (r & 7);
148              j += jinc - 3;
149 <            if (j >= n) j -= n;  
149 >            if (j >= n) j -= n;
150              if (j < 0) j += n;
151  
152              int l = n / 4 + j;
153 <            if (l >= n) l -= n;  
154 <      
153 >            if (l >= n) l -= n;
154 >
155              String k = key[j];
156 <      
156 >
157              if (!collection.contains(k)) {
158                  ++nagets;
159                  if (r < pctins) {
# Line 172 | Line 168 | public class CollectionWordLoops {
168                  if (r < pctrem) {
169                      collection.remove(k);
170                      ++nremoves;
171 <                    j += ((r >>> 8) & 7) +  n / 2;
171 >                    j += ((r >>> 8) & 7) + n / 2;
172                      if (j >= n) j -= n;
173                  }
174              }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines