ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CollectionWordLoops.java
Revision: 1.10
Committed: Thu Jan 15 18:34:18 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +0 -3 lines
Log Message:
delete extraneous blank lines

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.6 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6 jsr166 1.9 import java.io.*;
7 dl 1.1 import java.util.*;
8    
9     public class CollectionWordLoops {
10    
11     static final String[] WORDS_FILES = {
12 jsr166 1.3 "kw.txt",
13 dl 1.1 "class.txt",
14 dl 1.2 // "dir.txt",
15 jsr166 1.3 // "ids.txt",
16     // "/usr/dict/words",
17 dl 1.1 };
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 jsr166 1.8 Class<?> collectionClass = null;
27 dl 1.1 try {
28     collectionClass = Class.forName(args[0]);
29 jsr166 1.4 } catch (ClassNotFoundException e) {
30 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
31     }
32    
33     System.out.println("Testing " + collectionClass.getName());
34    
35 jsr166 1.3 for (int s = 0; s < WORDS_FILES.length; ++s)
36 dl 1.1 tests(collectionClass, numTests, s);
37    
38 jsr166 1.3 for (int s = WORDS_FILES.length-1; s >= 0; --s)
39 dl 1.1 tests(collectionClass, numTests, s);
40    
41     }
42    
43 jsr166 1.8 static void tests(Class<?> collectionClass, int numTests, int sizeIndex) {
44 jsr166 1.3 try {
45 dl 1.1 String[] key = readWords(sizeIndex);
46     int size = key.length;
47 jsr166 1.3
48 dl 1.1 System.out.print("n = " +LoopHelpers.rightJustify(size) +" : ");
49     long least = Long.MAX_VALUE;
50 jsr166 1.3
51 dl 1.1 for (int i = 0; i < numTests; ++i) {
52     Collection<String> m = newCollection(collectionClass);
53     long t = doTest(collectionClass.getName(), m, key);
54     if (t < least) least = t;
55     m.clear();
56     m = null;
57     }
58 jsr166 1.3
59 dl 1.1 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 jsr166 1.8 static Collection<String> newCollection(Class<?> cl) {
67 dl 1.1 try {
68 jsr166 1.5 Collection m = (Collection<String>) cl.newInstance();
69 dl 1.1 return m;
70 jsr166 1.4 } catch (Exception e) {
71 dl 1.1 throw new RuntimeException("Can't instantiate " + cl + ": " + e);
72     }
73     }
74    
75     static void pause() {
76 jsr166 1.4 try { Thread.sleep(100); }
77     catch (InterruptedException ie) { return; }
78 dl 1.1 }
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(String name,
108 jsr166 1.3 final Collection<String> m,
109 dl 1.1 final String[] key) {
110    
111     // System.out.print(name + "\t");
112     Runner runner = new Runner(m, key);
113     long startTime = System.currentTimeMillis();
114     runner.run();
115     long afterRun = System.currentTimeMillis();
116 jsr166 1.7 long runTime = afterRun - startTime;
117 dl 1.1 int np = runner.total;
118 jsr166 1.3 if (runner.total == runner.hashCode())
119 dl 1.1 System.out.println("Useless Number" + runner.total);
120     int sz = runner.maxsz;
121 jsr166 1.3 if (sz == runner.hashCode())
122 dl 1.1 System.out.println("Useless Number" + sz);
123     // System.out.print(" m = " + sz);
124     return runTime;
125     }
126    
127     static class Runner implements Runnable {
128     final Collection<String> collection;
129     final String[] key;
130     LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
131     final int pctrem;
132     final int pctins;
133     int nputs = 0;
134     int npgets = 0;
135     int nagets = 0;
136     int nremoves = 0;
137     volatile int total;
138     int maxsz;
139    
140     Runner(Collection<String> m, String[] k) {
141 jsr166 1.3 collection = m; key = k;
142 dl 1.1 pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50);
143     pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50);
144     }
145    
146     int oneStep(int j) {
147     int n = key.length;
148     int r = rng.next() & 0x7FFFFFFF;
149     int jinc = (r & 7);
150     j += jinc - 3;
151 jsr166 1.3 if (j >= n) j -= n;
152 dl 1.1 if (j < 0) j += n;
153    
154     int l = n / 4 + j;
155 jsr166 1.3 if (l >= n) l -= n;
156    
157 dl 1.1 String k = key[j];
158 jsr166 1.3
159 dl 1.1 if (!collection.contains(k)) {
160     ++nagets;
161     if (r < pctins) {
162     collection.add(k);
163     ++nputs;
164     int csz = nputs - nremoves;
165     if (csz > maxsz) maxsz = csz;
166     }
167     }
168     else {
169     ++npgets;
170     if (r < pctrem) {
171     collection.remove(k);
172     ++nremoves;
173 jsr166 1.7 j += ((r >>> 8) & 7) + n / 2;
174 dl 1.1 if (j >= n) j -= n;
175     }
176     }
177     return j;
178     }
179    
180     public void run() {
181     int j = key.length / 2;
182     for (int i = 0; i < NOPS; ++i) {
183     j = oneStep(j);
184     }
185     total = nputs + npgets + nagets + nremoves;
186     }
187     }
188    
189     }