ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CollectionWordLoops.java
Revision: 1.11
Committed: Mon Aug 10 03:13:33 2015 UTC (8 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +0 -1 lines
Log Message:
delete unwanted blank lines

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