ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MapWordLoops.java
Revision: 1.4
Committed: Fri Oct 23 19:57:06 2009 UTC (14 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.3: +4 -2 lines
Log Message:
Update misc tests for JDK7

File Contents

# Content
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
5 */
6 import java.util.*;
7 import java.io.*;
8
9 public class MapWordLoops {
10
11 static final String[] WORDS_FILES = {
12 "kw.txt",
13 "class.txt",
14 "dir.txt",
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 = 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;
29 try {
30 mapClass = Class.forName(args[0]);
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)
38 tests(mapClass, numTests, s);
39
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 {
47 String[] key = readWords(sizeIndex);
48 int size = key.length;
49
50 System.out.print("n = " +LoopHelpers.rightJustify(size) +" : ");
51 long least = Long.MAX_VALUE;
52
53 for (int i = 0; i < numTests; ++i) {
54 Map<String,String> m = newMap(mapClass);
55 long t = doTest(i, mapClass.getName(), m, key);
56 if (t < least) least = t;
57 m.clear();
58 m = null;
59 }
60
61 long nano = Math.round(1000000.0 * (least) / NOPS);
62 System.out.println(LoopHelpers.rightJustify(nano) + " ns per op");
63 } catch (IOException ignore) {
64 return; // skip test if can't read file
65 }
66 }
67
68
69 static Map<String,String> newMap(Class cl) {
70 try {
71 Map m = (Map<String,String>)cl.newInstance();
72 return m;
73 } catch(Exception e) {
74 throw new RuntimeException("Can't instantiate " + cl + ": " + e);
75 }
76 }
77
78 static void pause() {
79 try { Thread.sleep(100); } catch(InterruptedException ie) { return; }
80 }
81
82 static String[] readWords(int sizeIndex) throws IOException {
83 String[] l = new String[MAX_WORDS];
84 String[] array = null;
85 try {
86 FileReader fr = new FileReader(WORDS_FILES[sizeIndex]);
87 BufferedReader reader = new BufferedReader(fr);
88 int k = 0;
89 for (;;) {
90 String s = reader.readLine();
91 if (s == null) break;
92 l[k++] = s;
93 }
94 array = new String[k];
95 for (int i = 0; i < k; ++i) {
96 array[i] = l[i];
97 l[i] = null;
98 }
99 l = null;
100 reader.close();
101 }
102 catch (IOException ex) {
103 System.out.println("Can't read words file:" + ex);
104 throw ex;
105 }
106 return array;
107 }
108
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(id, m, key);
115 long startTime = System.currentTimeMillis();
116 runner.run();
117 long afterRun = System.currentTimeMillis();
118 long runTime = (afterRun - startTime);
119 int np = runner.total;
120 if (runner.total == runner.hashCode())
121 System.out.println("Useless Number" + runner.total);
122 int sz = runner.maxsz;
123 if (sz == runner.hashCode())
124 System.out.println("Useless Number" + sz);
125 // System.out.print(" m = " + sz);
126 return runTime;
127 }
128
129
130 static class Runner implements Runnable {
131 final Map<String,String> map;
132 final String[] key;
133 LoopHelpers.SimpleRandom rng;
134 final int pctrem;
135 final int pctins;
136 int nputs = 0;
137 int npgets = 0;
138 int nagets = 0;
139 int nremoves = 0;
140 volatile int total;
141 int maxsz;
142
143 Runner(int id, Map<String,String> m, String[] k) {
144 map = m; key = k;
145 pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50);
146 pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50);
147 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
148 }
149
150
151 int oneStep(int j) {
152 int n = key.length;
153 int r = rng.next() & 0x7FFFFFFF;
154 int jinc = (r & 7);
155 j += jinc - 3;
156 if (j >= n) j -= n;
157 if (j < 0) j += n;
158
159 int l = n / 4 + j;
160 if (l >= n) l -= n;
161
162 String k = key[j];
163 String x = map.get(k);
164
165 if (x == null) {
166 ++nagets;
167 if (r < pctins) {
168 map.put(k, key[l]);
169 ++nputs;
170 int csz = nputs - nremoves;
171 if (csz > maxsz) maxsz = csz;
172 }
173 }
174 else {
175 if (k== x) ++npgets;
176 if (r < pctrem) {
177 map.remove(k);
178 ++nremoves;
179 j += ((r >>> 8) & 7) + n / 2;
180 if (j >= n) j -= n;
181 }
182 }
183 return j;
184 }
185
186 public void run() {
187 int j = key.length / 2;
188 for (int i = 0; i < NOPS; ++i) {
189 j = oneStep(j);
190 }
191 total = nputs + npgets + nagets + nremoves;
192 }
193 }
194
195 }