ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MapWordLoops.java
Revision: 1.3
Committed: Sun Aug 7 19:25:55 2005 UTC (18 years, 9 months ago) by dl
Branch: MAIN
Changes since 1.2: +7 -6 lines
Log Message:
Add exchanger performance tests; update others

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 // "/usr/dict/words",
17 };
18
19 static final int MAX_WORDS = 500000;
20 static final int pinsert = 60;
21 static final int premove = 1;
22 static final int NOPS = 5000000;
23 static final int numTests = 3;
24
25 public static void main(String[] args) {
26 Class mapClass = null;
27 try {
28 mapClass = Class.forName(args[0]);
29 } catch(ClassNotFoundException e) {
30 throw new RuntimeException("Class " + args[0] + " not found.");
31 }
32
33 System.out.println("Testing " + mapClass.getName());
34
35 for (int s = 0; s < WORDS_FILES.length; ++s)
36 tests(mapClass, numTests, s);
37
38 for (int s = WORDS_FILES.length-1; s >= 0; --s)
39 tests(mapClass, numTests, s);
40
41 }
42
43 static void tests(Class mapClass, int numTests, int sizeIndex) {
44 try {
45 String[] key = readWords(sizeIndex);
46 int size = key.length;
47
48 System.out.print("n = " +LoopHelpers.rightJustify(size) +" : ");
49 long least = Long.MAX_VALUE;
50
51 for (int i = 0; i < numTests; ++i) {
52 Map<String,String> m = newMap(mapClass);
53 long t = doTest(i, mapClass.getName(), m, key);
54 if (t < least) least = t;
55 m.clear();
56 m = null;
57 }
58
59 long nano = Math.round(1000000.0 * (least) / NOPS);
60 System.out.println(LoopHelpers.rightJustify(nano) + " ns per op");
61 } catch (IOException ignore) {
62 return; // skip test if can't read file
63 }
64 }
65
66
67 static Map<String,String> newMap(Class cl) {
68 try {
69 Map m = (Map<String,String>)cl.newInstance();
70 return m;
71 } catch(Exception e) {
72 throw new RuntimeException("Can't instantiate " + cl + ": " + e);
73 }
74 }
75
76 static void pause() {
77 try { Thread.sleep(100); } catch(InterruptedException ie) { return; }
78 }
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 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(id, m, key);
113 long startTime = System.currentTimeMillis();
114 runner.run();
115 long afterRun = System.currentTimeMillis();
116 long runTime = (afterRun - startTime);
117 int np = runner.total;
118 if (runner.total == runner.hashCode())
119 System.out.println("Useless Number" + runner.total);
120 int sz = runner.maxsz;
121 if (sz == runner.hashCode())
122 System.out.println("Useless Number" + sz);
123 // System.out.print(" m = " + sz);
124 return runTime;
125 }
126
127
128 static class Runner implements Runnable {
129 final Map<String,String> map;
130 final String[] key;
131 LoopHelpers.SimpleRandom rng;
132 final int pctrem;
133 final int pctins;
134 int nputs = 0;
135 int npgets = 0;
136 int nagets = 0;
137 int nremoves = 0;
138 volatile int total;
139 int maxsz;
140
141 Runner(int id, Map<String,String> m, String[] k) {
142 map = m; key = k;
143 pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50);
144 pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50);
145 rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L);
146 }
147
148
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;
155 if (j < 0) j += n;
156
157 int l = n / 4 + j;
158 if (l >= n) l -= n;
159
160 String k = key[j];
161 String x = map.get(k);
162
163 if (x == null) {
164 ++nagets;
165 if (r < pctins) {
166 map.put(k, key[l]);
167 ++nputs;
168 int csz = nputs - nremoves;
169 if (csz > maxsz) maxsz = csz;
170 }
171 }
172 else {
173 if (k== x) ++npgets;
174 if (r < pctrem) {
175 map.remove(k);
176 ++nremoves;
177 j += ((r >>> 8) & 7) + n / 2;
178 if (j >= n) j -= n;
179 }
180 }
181 return j;
182 }
183
184 public void run() {
185 int j = key.length / 2;
186 for (int i = 0; i < NOPS; ++i) {
187 j = oneStep(j);
188 }
189 total = nputs + npgets + nagets + nremoves;
190 }
191 }
192
193 }