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.*; |
7 |
|
import java.io.*; |
9 |
|
public class CollectionWordLoops { |
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 |
> |
// "/usr/dict/words", |
17 |
|
}; |
18 |
|
|
19 |
|
static final int MAX_WORDS = 500000; |
26 |
|
Class collectionClass = null; |
27 |
|
try { |
28 |
|
collectionClass = Class.forName(args[0]); |
29 |
< |
} catch(ClassNotFoundException e) { |
29 |
> |
} catch (ClassNotFoundException e) { |
30 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
31 |
|
} |
32 |
|
|
33 |
|
System.out.println("Testing " + collectionClass.getName()); |
34 |
|
|
35 |
< |
for (int s = 0; s < WORDS_FILES.length; ++s) |
35 |
> |
for (int s = 0; s < WORDS_FILES.length; ++s) |
36 |
|
tests(collectionClass, numTests, s); |
37 |
|
|
38 |
< |
for (int s = WORDS_FILES.length-1; s >= 0; --s) |
38 |
> |
for (int s = WORDS_FILES.length-1; s >= 0; --s) |
39 |
|
tests(collectionClass, numTests, s); |
40 |
|
|
41 |
|
} |
42 |
|
|
43 |
|
static void tests(Class collectionClass, int numTests, int sizeIndex) { |
44 |
< |
try { |
44 |
> |
try { |
45 |
|
String[] key = readWords(sizeIndex); |
46 |
|
int size = key.length; |
47 |
< |
|
47 |
> |
|
48 |
|
System.out.print("n = " +LoopHelpers.rightJustify(size) +" : "); |
49 |
|
long least = Long.MAX_VALUE; |
50 |
< |
|
50 |
> |
|
51 |
|
for (int i = 0; i < numTests; ++i) { |
52 |
|
Collection<String> m = newCollection(collectionClass); |
53 |
|
long t = doTest(collectionClass.getName(), m, key); |
55 |
|
m.clear(); |
56 |
|
m = null; |
57 |
|
} |
58 |
< |
|
58 |
> |
|
59 |
|
long nano = Math.round(1000000.0 * (least) / NOPS); |
60 |
|
System.out.println(LoopHelpers.rightJustify(nano) + " ns per op"); |
61 |
|
} catch (IOException ignore) { |
66 |
|
|
67 |
|
static Collection<String> newCollection(Class cl) { |
68 |
|
try { |
69 |
< |
Collection m = (Collection<String>)cl.newInstance(); |
69 |
> |
Collection m = (Collection<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(String name, |
109 |
< |
final Collection<String> m, |
109 |
> |
final Collection<String> m, |
110 |
|
final String[] key) { |
111 |
|
|
112 |
|
// System.out.print(name + "\t"); |
116 |
|
long afterRun = System.currentTimeMillis(); |
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; |
140 |
|
int maxsz; |
141 |
|
|
142 |
|
Runner(Collection<String> m, String[] k) { |
143 |
< |
collection = m; key = k; |
143 |
> |
collection = m; key = k; |
144 |
|
pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50); |
145 |
|
pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50); |
146 |
|
} |
151 |
|
int r = rng.next() & 0x7FFFFFFF; |
152 |
|
int jinc = (r & 7); |
153 |
|
j += jinc - 3; |
154 |
< |
if (j >= n) j -= n; |
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 |
< |
|
158 |
> |
if (l >= n) l -= n; |
159 |
> |
|
160 |
|
String k = key[j]; |
161 |
< |
|
161 |
> |
|
162 |
|
if (!collection.contains(k)) { |
163 |
|
++nagets; |
164 |
|
if (r < pctins) { |