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.13 by jsr166, Sun Oct 23 03:03:23 2016 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);
35
42      }
43  
44 <    static void tests(Class mapClass, int numTests, int sizeIndex) {
45 <        try {
44 >    static void tests(Class<?> mapClass, int numTests, int sizeIndex) {
45 >        try {
46              String[] key = readWords(sizeIndex);
47              int size = key.length;
48 <            
48 >
49              System.out.print("n = " +LoopHelpers.rightJustify(size) +" : ");
50              long least = Long.MAX_VALUE;
51 <            
51 >
52              for (int i = 0; i < numTests; ++i) {
53                  Map<String,String> m = newMap(mapClass);
54 <                long t = doTest(mapClass.getName(), m, key);
54 >                long t = doTest(i, mapClass.getName(), m, key);
55                  if (t < least) least = t;
56                  m.clear();
57                  m = null;
58              }
59 <            
59 >
60              long nano = Math.round(1000000.0 * (least) / NOPS);
61              System.out.println(LoopHelpers.rightJustify(nano) + " ns per op");
62          } catch (IOException ignore) {
# Line 58 | Line 64 | public class MapWordLoops {
64          }
65      }
66  
67 <
62 <    static Map<String,String> newMap(Class cl) {
67 >    static Map<String,String> newMap(Class<?> cl) {
68          try {
69 <            Map m = (Map<String,String>)cl.newInstance();
70 <            return m;
66 <        } catch(Exception e) {
69 >            return (Map<String,String>) cl.getConstructor().newInstance();
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 99 | Line 104 | public class MapWordLoops {
104          return array;
105      }
106  
107 <    static long doTest(String name,
108 <                       final Map<String,String> m,
107 >    static long doTest(int id, String name,
108 >                       final Map<String,String> m,
109                         final String[] key) {
110  
111          //    System.out.print(name + "\t");
112 <        Runner runner = new Runner(m, key);
112 >        Runner runner = new Runner(id, m, key);
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  
122
127      static class Runner implements Runnable {
128          final Map<String,String> map;
129          final String[] key;
130 <        LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
130 >        LoopHelpers.SimpleRandom rng;
131          final int pctrem;
132          final int pctins;
133          int nputs = 0;
# Line 133 | Line 137 | public class MapWordLoops {
137          volatile int total;
138          int maxsz;
139  
140 <        Runner(Map<String,String> m, String[] k) {
141 <            map = m; key = k;
140 >        Runner(int id, Map<String,String> m, String[] k) {
141 >            map = 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 +            rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
145          }
146  
142
147          int oneStep(int j) {
148              int n = key.length;
149              int r = rng.next() & 0x7FFFFFFF;
150              int jinc = (r & 7);
151              j += jinc - 3;
152 <            if (j >= n) j -= n;  
152 >            if (j >= n) j -= n;
153              if (j < 0) j += n;
154  
155              int l = n / 4 + j;
156 <            if (l >= n) l -= n;  
157 <      
156 >            if (l >= n) l -= n;
157 >
158              String k = key[j];
159              String x = map.get(k);
160 <      
160 >
161              if (x == null) {
162                  ++nagets;
163                  if (r < pctins) {
# Line 168 | Line 172 | public class MapWordLoops {
172                  if (r < pctrem) {
173                      map.remove(k);
174                      ++nremoves;
175 <                    j += ((r >>> 8) & 7) +  n / 2;
175 >                    j += ((r >>> 8) & 7) + n / 2;
176                      if (j >= n) j -= n;
177                  }
178              }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines