ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CollectionWordLoops.java
Revision: 1.12
Committed: Sun Oct 23 03:03:23 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +1 -2 lines
Log Message:
fix deprecation warnings for Class#newInstance

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