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); |
42 |
|
|
43 |
|
} |
44 |
|
|
45 |
< |
static void tests(Class mapClass, int numTests, int sizeIndex) { |
46 |
< |
try { |
45 |
> |
static void tests(Class<?> mapClass, int numTests, int sizeIndex) { |
46 |
> |
try { |
47 |
|
String[] key = readWords(sizeIndex); |
48 |
|
int size = key.length; |
49 |
< |
|
49 |
> |
|
50 |
|
System.out.print("n = " +LoopHelpers.rightJustify(size) +" : "); |
51 |
|
long least = Long.MAX_VALUE; |
52 |
< |
|
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); |
57 |
|
m.clear(); |
58 |
|
m = null; |
59 |
|
} |
60 |
< |
|
60 |
> |
|
61 |
|
long nano = Math.round(1000000.0 * (least) / NOPS); |
62 |
|
System.out.println(LoopHelpers.rightJustify(nano) + " ns per op"); |
63 |
|
} catch (IOException ignore) { |
66 |
|
} |
67 |
|
|
68 |
|
|
69 |
< |
static Map<String,String> newMap(Class cl) { |
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) { |
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; } |
79 |
> |
try { Thread.sleep(100); } |
80 |
> |
catch (InterruptedException ie) { return; } |
81 |
|
} |
82 |
|
|
83 |
|
static String[] readWords(int sizeIndex) throws IOException { |
108 |
|
} |
109 |
|
|
110 |
|
static long doTest(int id, String name, |
111 |
< |
final Map<String,String> m, |
111 |
> |
final Map<String,String> m, |
112 |
|
final String[] key) { |
113 |
|
|
114 |
|
// System.out.print(name + "\t"); |
116 |
|
long startTime = System.currentTimeMillis(); |
117 |
|
runner.run(); |
118 |
|
long afterRun = System.currentTimeMillis(); |
119 |
< |
long runTime = (afterRun - startTime); |
119 |
> |
long runTime = afterRun - startTime; |
120 |
|
int np = runner.total; |
121 |
< |
if (runner.total == runner.hashCode()) |
121 |
> |
if (runner.total == runner.hashCode()) |
122 |
|
System.out.println("Useless Number" + runner.total); |
123 |
|
int sz = runner.maxsz; |
124 |
< |
if (sz == runner.hashCode()) |
124 |
> |
if (sz == runner.hashCode()) |
125 |
|
System.out.println("Useless Number" + sz); |
126 |
|
// System.out.print(" m = " + sz); |
127 |
|
return runTime; |
142 |
|
int maxsz; |
143 |
|
|
144 |
|
Runner(int id, Map<String,String> m, String[] k) { |
145 |
< |
map = m; key = k; |
145 |
> |
map = m; key = k; |
146 |
|
pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50); |
147 |
|
pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50); |
148 |
|
rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); |
154 |
|
int r = rng.next() & 0x7FFFFFFF; |
155 |
|
int jinc = (r & 7); |
156 |
|
j += jinc - 3; |
157 |
< |
if (j >= n) j -= n; |
157 |
> |
if (j >= n) j -= n; |
158 |
|
if (j < 0) j += n; |
159 |
|
|
160 |
|
int l = n / 4 + j; |
161 |
< |
if (l >= n) l -= n; |
162 |
< |
|
161 |
> |
if (l >= n) l -= n; |
162 |
> |
|
163 |
|
String k = key[j]; |
164 |
|
String x = map.get(k); |
165 |
< |
|
165 |
> |
|
166 |
|
if (x == null) { |
167 |
|
++nagets; |
168 |
|
if (r < pctins) { |
177 |
|
if (r < pctrem) { |
178 |
|
map.remove(k); |
179 |
|
++nremoves; |
180 |
< |
j += ((r >>> 8) & 7) + n / 2; |
180 |
> |
j += ((r >>> 8) & 7) + n / 2; |
181 |
|
if (j >= n) j -= n; |
182 |
|
} |
183 |
|
} |