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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
– |
import java.util.*; |
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); |
40 |
– |
|
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(i, mapClass.getName(), m, key); |
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) { |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
< |
|
67 |
< |
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; |
71 |
< |
} catch(Exception e) { |
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; } |
77 |
> |
try { Thread.sleep(100); } |
78 |
> |
catch (InterruptedException ie) { return; } |
79 |
|
} |
80 |
|
|
81 |
|
static String[] readWords(int sizeIndex) throws IOException { |
106 |
|
} |
107 |
|
|
108 |
|
static long doTest(int id, String name, |
109 |
< |
final Map<String,String> m, |
109 |
> |
final Map<String,String> m, |
110 |
|
final String[] key) { |
111 |
|
|
112 |
|
// System.out.print(name + "\t"); |
114 |
|
long startTime = System.currentTimeMillis(); |
115 |
|
runner.run(); |
116 |
|
long afterRun = System.currentTimeMillis(); |
117 |
< |
long runTime = (afterRun - startTime); |
117 |
> |
long runTime = afterRun - startTime; |
118 |
|
int np = runner.total; |
119 |
< |
if (runner.total == runner.hashCode()) |
119 |
> |
if (runner.total == runner.hashCode()) |
120 |
|
System.out.println("Useless Number" + runner.total); |
121 |
|
int sz = runner.maxsz; |
122 |
< |
if (sz == runner.hashCode()) |
122 |
> |
if (sz == runner.hashCode()) |
123 |
|
System.out.println("Useless Number" + sz); |
124 |
|
// System.out.print(" m = " + sz); |
125 |
|
return runTime; |
126 |
|
} |
127 |
|
|
127 |
– |
|
128 |
|
static class Runner implements Runnable { |
129 |
|
final Map<String,String> map; |
130 |
|
final String[] key; |
139 |
|
int maxsz; |
140 |
|
|
141 |
|
Runner(int id, Map<String,String> m, String[] k) { |
142 |
< |
map = m; key = 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 |
– |
|
148 |
|
int oneStep(int j) { |
149 |
|
int n = key.length; |
150 |
|
int r = rng.next() & 0x7FFFFFFF; |
151 |
|
int jinc = (r & 7); |
152 |
|
j += jinc - 3; |
153 |
< |
if (j >= n) j -= n; |
153 |
> |
if (j >= n) j -= n; |
154 |
|
if (j < 0) j += n; |
155 |
|
|
156 |
|
int l = n / 4 + j; |
157 |
< |
if (l >= n) l -= n; |
158 |
< |
|
157 |
> |
if (l >= n) l -= n; |
158 |
> |
|
159 |
|
String k = key[j]; |
160 |
|
String x = map.get(k); |
161 |
< |
|
161 |
> |
|
162 |
|
if (x == null) { |
163 |
|
++nagets; |
164 |
|
if (r < pctins) { |
173 |
|
if (r < pctrem) { |
174 |
|
map.remove(k); |
175 |
|
++nremoves; |
176 |
< |
j += ((r >>> 8) & 7) + n / 2; |
176 |
> |
j += ((r >>> 8) & 7) + n / 2; |
177 |
|
if (j >= n) j -= n; |
178 |
|
} |
179 |
|
} |