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

Comparing jsr166/src/test/loops/MapWordLoops.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.11 by jsr166, Thu Jan 15 18:34:19 2015 UTC

# Line 1 | Line 1
1 < import java.util.*;
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/publicdomain/zero/1.0/
5 > */
6   import java.io.*;
7 + import java.util.*;
8  
9   public class MapWordLoops {
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",
15 >        "ids.txt",
16 >        "testwords.txt",
17 >        //        "/usr/dict/words",
18      };
19  
20      static final int MAX_WORDS = 500000;
21      static final int pinsert   = 60;
22 <    static final int premove   = 1;
23 <    static final int NOPS      = 5000000;
22 >    static final int premove   = 2;
23 >    static final int NOPS      = 8000000;
24 >
25      static final int numTests = 3;
26  
27      public static void main(String[] args) {
28 <        Class mapClass = null;
28 >        Class<?> mapClass = null;
29          try {
30              mapClass = Class.forName(args[0]);
31 <        } catch(ClassNotFoundException e) {
31 >        } catch (ClassNotFoundException e) {
32              throw new RuntimeException("Class " + args[0] + " not found.");
33          }
34  
35          System.out.println("Testing " + mapClass.getName());
36  
37 <        for (int s = 0; s < WORDS_FILES.length; ++s)
37 >        for (int s = 0; s < WORDS_FILES.length; ++s)
38              tests(mapClass, numTests, s);
39  
40 <        for (int s = WORDS_FILES.length-1; s >= 0; --s)
40 >        for (int s = WORDS_FILES.length-1; s >= 0; --s)
41              tests(mapClass, numTests, s);
42  
43      }
44  
45 <    static void tests(Class mapClass, int numTests, int sizeIndex) {
46 <        try {
45 >    static void tests(Class<?> mapClass, int numTests, int sizeIndex) {
46 >        try {
47              String[] key = readWords(sizeIndex);
48              int size = key.length;
49 <            
49 >
50              System.out.print("n = " +LoopHelpers.rightJustify(size) +" : ");
51              long least = Long.MAX_VALUE;
52 <            
52 >
53              for (int i = 0; i < numTests; ++i) {
54                  Map<String,String> m = newMap(mapClass);
55 <                long t = doTest(mapClass.getName(), m, key);
55 >                long t = doTest(i, mapClass.getName(), m, key);
56                  if (t < least) least = t;
57                  m.clear();
58                  m = null;
59              }
60 <            
60 >
61              long nano = Math.round(1000000.0 * (least) / NOPS);
62              System.out.println(LoopHelpers.rightJustify(nano) + " ns per op");
63          } catch (IOException ignore) {
# Line 58 | Line 65 | public class MapWordLoops {
65          }
66      }
67  
68 <
62 <    static Map<String,String> newMap(Class cl) {
68 >    static Map<String,String> newMap(Class<?> cl) {
69          try {
70              Map m = (Map<String,String>)cl.newInstance();
71              return m;
72 <        } catch(Exception e) {
72 >        } catch (Exception e) {
73              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
74          }
75      }
76  
77      static void pause() {
78 <        try { Thread.sleep(100); } catch(InterruptedException ie) { return; }
78 >        try { Thread.sleep(100); }
79 >        catch (InterruptedException ie) { return; }
80      }
81  
82      static String[] readWords(int sizeIndex) throws IOException {
# Line 99 | Line 106 | public class MapWordLoops {
106          return array;
107      }
108  
109 <    static long doTest(String name,
110 <                       final Map<String,String> m,
109 >    static long doTest(int id, String name,
110 >                       final Map<String,String> m,
111                         final String[] key) {
112  
113          //    System.out.print(name + "\t");
114 <        Runner runner = new Runner(m, key);
114 >        Runner runner = new Runner(id, m, key);
115          long startTime = System.currentTimeMillis();
116          runner.run();
117          long afterRun = System.currentTimeMillis();
118 <        long runTime =  (afterRun - startTime);
118 >        long runTime = afterRun - startTime;
119          int np = runner.total;
120 <        if (runner.total == runner.hashCode())
120 >        if (runner.total == runner.hashCode())
121              System.out.println("Useless Number" + runner.total);
122          int sz = runner.maxsz;
123 <        if (sz == runner.hashCode())
123 >        if (sz == runner.hashCode())
124              System.out.println("Useless Number" + sz);
125          //        System.out.print(" m = " + sz);
126          return runTime;
127      }
128  
122
129      static class Runner implements Runnable {
130          final Map<String,String> map;
131          final String[] key;
132 <        LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
132 >        LoopHelpers.SimpleRandom rng;
133          final int pctrem;
134          final int pctins;
135          int nputs = 0;
# Line 133 | Line 139 | public class MapWordLoops {
139          volatile int total;
140          int maxsz;
141  
142 <        Runner(Map<String,String> m, String[] k) {
143 <            map = m; key = k;
142 >        Runner(int id, Map<String,String> m, String[] k) {
143 >            map = m; key = k;
144              pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50);
145              pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50);
146 +            rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
147          }
148  
142
149          int oneStep(int j) {
150              int n = key.length;
151              int r = rng.next() & 0x7FFFFFFF;
152              int jinc = (r & 7);
153              j += jinc - 3;
154 <            if (j >= n) j -= n;  
154 >            if (j >= n) j -= n;
155              if (j < 0) j += n;
156  
157              int l = n / 4 + j;
158 <            if (l >= n) l -= n;  
159 <      
158 >            if (l >= n) l -= n;
159 >
160              String k = key[j];
161              String x = map.get(k);
162 <      
162 >
163              if (x == null) {
164                  ++nagets;
165                  if (r < pctins) {
# Line 168 | Line 174 | public class MapWordLoops {
174                  if (r < pctrem) {
175                      map.remove(k);
176                      ++nremoves;
177 <                    j += ((r >>> 8) & 7) +  n / 2;
177 >                    j += ((r >>> 8) & 7) + n / 2;
178                      if (j >= n) j -= n;
179                  }
180              }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines