ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/IntegerMax.java
Revision: 1.2
Committed: Thu Feb 28 16:33:53 2013 UTC (11 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +4 -4 lines
Log Message:
whitespace

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
7 import java.util.*;
8 import java.util.function.*;
9 import java.util.concurrent.*;
10 import java.util.concurrent.atomic.*;
11
12 public class IntegerMax {
13 static final int SIZE = 1000000;
14 static final AtomicInteger checksum = new AtomicInteger();
15 static boolean print;
16 static boolean allClasses = false; // true if also test dumb/default classes
17 static final BinaryOperator<Integer> MAX =
18 (Integer x, Integer y) -> x >= y ? x : y;
19
20 public static void main(String[] args) throws Exception {
21 if (args.length > 0)
22 allClasses = true;
23 print = false;
24 System.out.println("warmup...");
25 allTests(10000, 100);
26 System.out.println("...");
27 print = true;
28 int step = 10;
29 // int step = 100;
30 for (int reps = 0; reps < 2; ++reps) {
31 int trials = SIZE;
32 for (int size = 1; size <= SIZE; size *= step) {
33 allTests(size, trials);
34 trials /= (step / 2);
35 }
36 }
37 }
38
39 static String sep() { return print ? "\n" : " "; }
40
41 static void allTests(int size, int trials) throws Exception {
42 System.out.println("---------------------------------------------");
43 System.out.println("size: " + size + " trials: " + trials);
44 Integer[] keys = new Integer[size];
45 for (int i = 0; i < size; ++i)
46 keys[i] = Integer.valueOf(i);
47 Integer[] vals = Arrays.copyOf(keys, size);
48 shuffle(keys);
49 shuffle(vals);
50 List<Integer> klist = Arrays.asList(keys);
51 List<Integer> vlist = Arrays.asList(vals);
52 int kmax = size - 1;
53 int vmax = size - 1;
54
55 isptest(klist, kmax, size, trials);
56 System.out.print("Arrays.asList.keys" + sep());
57 isptest(vlist, vmax, size, trials);
58 System.out.print("Arrays.asList.values" + sep());
59
60 ctest(new ArrayList<Integer>(), klist, kmax, size, trials);
61 ctest(new ArrayList<Integer>(), vlist, vmax, size, trials);
62 ctest(new Vector<Integer>(), klist, kmax, size, trials);
63 ctest(new Vector<Integer>(), vlist, vmax, size, trials);
64 ctest(new ArrayDeque<Integer>(), klist, kmax, size, trials);
65 ctest(new ArrayDeque<Integer>(), vlist, vmax, size, trials);
66 ctest(new CopyOnWriteArrayList<Integer>(), klist, kmax, size, trials);
67 ctest(new CopyOnWriteArrayList<Integer>(), vlist, vmax, size, trials);
68 ctest(new PriorityQueue<Integer>(), klist, kmax, size, trials);
69 ctest(new PriorityQueue<Integer>(), vlist, vmax, size, trials);
70 ctest(new HashSet<Integer>(), klist, kmax, size, trials);
71 ctest(new HashSet<Integer>(), vlist, vmax, size, trials);
72 ctest(ConcurrentHashMap.<Integer>newKeySet(), klist, kmax, size, trials);
73 ctest(ConcurrentHashMap.<Integer>newKeySet(), vlist, vmax, size, trials);
74 ctest(new TreeSet<Integer>(), klist, kmax, size, trials);
75 ctest(new TreeSet<Integer>(), vlist, vmax, size, trials);
76 ctest(ConcurrentSkipListMap.<Integer>newKeySet(), klist, kmax, size, trials);
77 ctest(ConcurrentSkipListMap.<Integer>newKeySet(), vlist, vmax, size, trials);
78
79 mtest(new HashMap<Integer,Integer>(), keys, vals, kmax, vmax, size, trials);
80 mtest(new IdentityHashMap<Integer,Integer>(), keys, vals, kmax, vmax, size, trials);
81 mtest(new WeakHashMap<Integer,Integer>(), keys, vals, kmax, vmax, size, trials);
82 mtest(new ConcurrentHashMap<Integer,Integer>(), keys, vals, kmax, vmax, size, trials);
83
84 mtest(new TreeMap<Integer,Integer>(), keys, vals, kmax, vmax, size, trials);
85 mtest(new ConcurrentSkipListMap<Integer,Integer>(), keys, vals, kmax, vmax, size, trials);
86
87 if (allClasses) {
88 mtest(new Hashtable<Integer,Integer>(), keys, vals, kmax, vmax, size, trials);
89 mtest(new LinkedHashMap<Integer,Integer>(), keys, vals, kmax, vmax, size, trials);
90 ctest(new LinkedHashSet<Integer>(), klist, kmax, size, trials);
91 ctest(new LinkedHashSet<Integer>(), vlist, vmax, size, trials);
92 ctest(new LinkedList<Integer>(), klist, kmax, size, trials);
93 ctest(new LinkedList<Integer>(), vlist, vmax, size, trials);
94 ctest(new ConcurrentLinkedQueue<Integer>(), klist, kmax, size, trials);
95 ctest(new ConcurrentLinkedQueue<Integer>(), vlist, vmax, size, trials);
96 ctest(new ConcurrentLinkedDeque<Integer>(), klist, kmax, size, trials);
97 ctest(new ConcurrentLinkedDeque<Integer>(), vlist, vmax, size, trials);
98 ctest(new LinkedBlockingQueue<Integer>(SIZE), klist, kmax, size, trials);
99 ctest(new LinkedBlockingQueue<Integer>(SIZE), vlist, vmax, size, trials);
100 ctest(new LinkedBlockingDeque<Integer>(SIZE), klist, kmax, size, trials);
101 ctest(new LinkedBlockingDeque<Integer>(SIZE), vlist, vmax, size, trials);
102 ctest(new LinkedTransferQueue<Integer>(), klist, kmax, size, trials);
103 ctest(new LinkedTransferQueue<Integer>(), vlist, vmax, size, trials);
104 ctest(new ArrayBlockingQueue<Integer>(SIZE), klist, kmax, size, trials);
105 ctest(new ArrayBlockingQueue<Integer>(SIZE), vlist, vmax, size, trials);
106 ctest(new PriorityBlockingQueue<Integer>(SIZE), klist, kmax, size, trials);
107 ctest(new PriorityBlockingQueue<Integer>(SIZE), vlist, vmax, size, trials);
108 }
109
110 if (checksum.get() != 0)
111 throw new Error("bad computation");
112 }
113
114 static void ctest(Collection<Integer> c, List<Integer> klist, int kmax, int size, int trials)
115 throws Exception {
116 String cn = c.getClass().getName();
117 if (cn.startsWith("java.util.concurrent."))
118 cn = cn.substring(21);
119 else if (cn.startsWith("java.util."))
120 cn = cn.substring(10);
121 c.addAll(klist);
122 isptest(c, kmax, size, trials);
123 System.out.print(cn + sep());
124 }
125
126 static void mtest(Map<Integer,Integer> m, Integer[] keys, Integer[] vals, int kmax, int vmax, int size, int trials) throws Exception {
127 String cn = m.getClass().getName();
128 if (cn.startsWith("java.util.concurrent."))
129 cn = cn.substring(21);
130 else if (cn.startsWith("java.util."))
131 cn = cn.substring(10);
132 for (int i = 0; i < size; ++i)
133 m.put(keys[i], vals[i]);
134 isptest(m.keySet(), kmax, size, trials);
135 System.out.print(cn + ".keys" + sep());
136 isptest(m.values(), vmax, size, trials);
137 System.out.print(cn + ".vals" + sep());
138 }
139
140 static void isptest(Collection<Integer> c, int max, int size, int trials) throws Exception {
141 long ti = itest(c, max, trials);
142 long ts = stest(c, max, trials);
143 long tp = ptest(c, max, trials);
144 if (print) {
145 long scale = (long)size * trials;
146 double di = ((double)ti) / scale;
147 double ds = ((double)ts) / scale;
148 double dp = ((double)tp) / scale;
149 System.out.printf("n:%7d ", size);
150 System.out.printf("i:%8.2f ", di);
151 System.out.printf("s:%8.2f ", ds);
152 System.out.printf("p:%8.2f ", dp);
153 }
154 }
155
156 static long itest(Collection<Integer> c, int max, int trials) throws Exception {
157 if (c == null) throw new Error();
158 Thread.sleep(250);
159 long tlast = System.nanoTime();
160 for (int i = 0; i < trials; ++i) {
161 Integer pmax = Integer.valueOf(Integer.MIN_VALUE - checksum.get());
162 for (Integer x : c)
163 pmax = MAX.apply(pmax, x);
164 checksum.getAndAdd(max - pmax);
165 }
166 return System.nanoTime() - tlast;
167 }
168
169 static long stest(Collection<Integer> c, int max, int trials) throws Exception {
170 if (c == null) throw new Error();
171 Thread.sleep(250);
172 long tlast = System.nanoTime();
173 for (int i = 0; i < trials; ++i) {
174 int pmax = c.stream().reduce
175 (Integer.valueOf(Integer.MIN_VALUE - checksum.get()), MAX);
176 checksum.getAndAdd(max - pmax);
177 }
178 return System.nanoTime() - tlast;
179 }
180
181 static long ptest(Collection<Integer> c, int max, int trials) throws Exception {
182 if (c == null) throw new Error();
183 Thread.sleep(250);
184 long tlast = System.nanoTime();
185 for (int i = 0; i < trials; ++i) {
186 int pmax = c.parallelStream().reduce
187 (Integer.valueOf(Integer.MIN_VALUE - checksum.get()), MAX);
188 checksum.getAndAdd(max - pmax);
189 }
190 return System.nanoTime() - tlast;
191 }
192
193 // misc
194
195 static final long NPS = (1000L * 1000 * 1000);
196 static double elapsedTime(long startTime) {
197 return (double)(System.nanoTime() - startTime) / NPS;
198 }
199
200 static void shuffle(Object[] a) {
201 ThreadLocalRandom rng = ThreadLocalRandom.current();
202 for (int i = a.length; i > 1; i--) {
203 Object t = a[i-1];
204 int r = rng.nextInt(i);
205 a[i-1] = a[r];
206 a[r] = t;
207 }
208 }
209
210 }