ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MapWordLoops.java
Revision: 1.13
Committed: Sun Oct 23 03:03:23 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +1 -2 lines
Log Message:
fix deprecation warnings for Class#newInstance

File Contents

# User Rev Content
1 dl 1.2 /*
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 jsr166 1.7 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6 jsr166 1.10 import java.io.*;
7 dl 1.1 import java.util.*;
8    
9     public class MapWordLoops {
10    
11     static final String[] WORDS_FILES = {
12 jsr166 1.5 "kw.txt",
13 dl 1.1 "class.txt",
14     "dir.txt",
15 jsr166 1.5 "ids.txt",
16 dl 1.4 "testwords.txt",
17 jsr166 1.5 // "/usr/dict/words",
18 dl 1.1 };
19    
20     static final int MAX_WORDS = 500000;
21     static final int pinsert = 60;
22 dl 1.4 static final int premove = 2;
23     static final int NOPS = 8000000;
24    
25 dl 1.1 static final int numTests = 3;
26    
27     public static void main(String[] args) {
28 jsr166 1.9 Class<?> mapClass = null;
29 dl 1.1 try {
30     mapClass = Class.forName(args[0]);
31 jsr166 1.6 } catch (ClassNotFoundException e) {
32 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
33     }
34    
35     System.out.println("Testing " + mapClass.getName());
36    
37 jsr166 1.5 for (int s = 0; s < WORDS_FILES.length; ++s)
38 dl 1.1 tests(mapClass, numTests, s);
39    
40 jsr166 1.5 for (int s = WORDS_FILES.length-1; s >= 0; --s)
41 dl 1.1 tests(mapClass, numTests, s);
42     }
43    
44 jsr166 1.9 static void tests(Class<?> mapClass, int numTests, int sizeIndex) {
45 jsr166 1.5 try {
46 dl 1.1 String[] key = readWords(sizeIndex);
47     int size = key.length;
48 jsr166 1.5
49 dl 1.1 System.out.print("n = " +LoopHelpers.rightJustify(size) +" : ");
50     long least = Long.MAX_VALUE;
51 jsr166 1.5
52 dl 1.1 for (int i = 0; i < numTests; ++i) {
53     Map<String,String> m = newMap(mapClass);
54 dl 1.3 long t = doTest(i, mapClass.getName(), m, key);
55 dl 1.1 if (t < least) least = t;
56     m.clear();
57     m = null;
58     }
59 jsr166 1.5
60 dl 1.1 long nano = Math.round(1000000.0 * (least) / NOPS);
61     System.out.println(LoopHelpers.rightJustify(nano) + " ns per op");
62     } catch (IOException ignore) {
63     return; // skip test if can't read file
64     }
65     }
66    
67 jsr166 1.9 static Map<String,String> newMap(Class<?> cl) {
68 dl 1.1 try {
69 jsr166 1.13 return (Map<String,String>) cl.getConstructor().newInstance();
70 jsr166 1.6 } catch (Exception e) {
71 dl 1.1 throw new RuntimeException("Can't instantiate " + cl + ": " + e);
72     }
73     }
74    
75     static void pause() {
76 jsr166 1.6 try { Thread.sleep(100); }
77     catch (InterruptedException ie) { return; }
78 dl 1.1 }
79    
80     static String[] readWords(int sizeIndex) throws IOException {
81     String[] l = new String[MAX_WORDS];
82     String[] array = null;
83     try {
84     FileReader fr = new FileReader(WORDS_FILES[sizeIndex]);
85     BufferedReader reader = new BufferedReader(fr);
86     int k = 0;
87     for (;;) {
88     String s = reader.readLine();
89     if (s == null) break;
90     l[k++] = s;
91     }
92     array = new String[k];
93     for (int i = 0; i < k; ++i) {
94     array[i] = l[i];
95     l[i] = null;
96     }
97     l = null;
98     reader.close();
99     }
100     catch (IOException ex) {
101     System.out.println("Can't read words file:" + ex);
102     throw ex;
103     }
104     return array;
105     }
106    
107 dl 1.3 static long doTest(int id, String name,
108 jsr166 1.5 final Map<String,String> m,
109 dl 1.1 final String[] key) {
110    
111     // System.out.print(name + "\t");
112 dl 1.3 Runner runner = new Runner(id, m, key);
113 dl 1.1 long startTime = System.currentTimeMillis();
114     runner.run();
115     long afterRun = System.currentTimeMillis();
116 jsr166 1.8 long runTime = afterRun - startTime;
117 dl 1.1 int np = runner.total;
118 jsr166 1.5 if (runner.total == runner.hashCode())
119 dl 1.1 System.out.println("Useless Number" + runner.total);
120     int sz = runner.maxsz;
121 jsr166 1.5 if (sz == runner.hashCode())
122 dl 1.1 System.out.println("Useless Number" + sz);
123     // System.out.print(" m = " + sz);
124     return runTime;
125     }
126    
127     static class Runner implements Runnable {
128     final Map<String,String> map;
129     final String[] key;
130 dl 1.3 LoopHelpers.SimpleRandom rng;
131 dl 1.1 final int pctrem;
132     final int pctins;
133     int nputs = 0;
134     int npgets = 0;
135     int nagets = 0;
136     int nremoves = 0;
137     volatile int total;
138     int maxsz;
139    
140 dl 1.3 Runner(int id, Map<String,String> m, String[] k) {
141 jsr166 1.5 map = m; key = k;
142 dl 1.1 pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50);
143     pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50);
144 dl 1.3 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
145 dl 1.1 }
146    
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 jsr166 1.5 if (j >= n) j -= n;
153 dl 1.1 if (j < 0) j += n;
154    
155     int l = n / 4 + j;
156 jsr166 1.5 if (l >= n) l -= n;
157    
158 dl 1.1 String k = key[j];
159     String x = map.get(k);
160 jsr166 1.5
161 dl 1.1 if (x == null) {
162     ++nagets;
163     if (r < pctins) {
164     map.put(k, key[l]);
165     ++nputs;
166     int csz = nputs - nremoves;
167     if (csz > maxsz) maxsz = csz;
168     }
169     }
170     else {
171     if (k== x) ++npgets;
172     if (r < pctrem) {
173     map.remove(k);
174     ++nremoves;
175 jsr166 1.8 j += ((r >>> 8) & 7) + n / 2;
176 dl 1.1 if (j >= n) j -= n;
177     }
178     }
179     return j;
180     }
181    
182     public void run() {
183     int j = key.length / 2;
184     for (int i = 0; i < NOPS; ++i) {
185     j = oneStep(j);
186     }
187     total = nputs + npgets + nagets + nremoves;
188     }
189     }
190    
191     }