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

File Contents

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