--- jsr166/src/test/loops/MapMicroBenchmark.java 2010/09/01 07:20:36 1.9 +++ jsr166/src/test/loops/MapMicroBenchmark.java 2016/10/23 03:03:23 1.18 @@ -1,12 +1,12 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ -import java.util.*; import java.io.*; import java.math.*; +import java.util.*; /** * A micro-benchmark with key types and operation mixes roughly @@ -38,7 +38,7 @@ import java.math.*; public class MapMicroBenchmark { static final String wordFile = "testwords.txt"; - static Class mapClass; + static Class mapClass; static boolean randomSearches = true; // Nanoseconds per run @@ -147,7 +147,7 @@ public class MapMicroBenchmark { } static void runWork(Job[] jobs, int minIters, int maxIters, long timeLimit) throws Throwable { - for (int k = 0; k < nsizes; ++k) { + for (int k = 0; k < nsizes; ++k) { int len = sizes[k]; for (int i = 0; i < jobs.length; i++) { Thread.sleep(50); @@ -160,7 +160,7 @@ public class MapMicroBenchmark { // First warmup -- run only mixed job to discourage type specialization static void warmup1(Job job) throws Throwable { - for (int k = 0; k < nsizes; ++k) + for (int k = 0; k < nsizes; ++k) job.work(sizes[k], 1, 1, 0); } @@ -210,16 +210,15 @@ public class MapMicroBenchmark { System.out.println("\n"); } - static final class Job { final String name; - final Class elementClass; + final Class elementClass; long[] nanos = new long[nsizes]; final Object[] items; Object[] searches; volatile long checkSum; volatile int lastSum; - Job(String name, Object[] items, Class elementClass) { + Job(String name, Object[] items, Class elementClass) { this.name = name; this.items = items; this.elementClass = elementClass; @@ -234,9 +233,9 @@ public class MapMicroBenchmark { } public long work(int len, int minIters, int maxIters, long timeLimit) { - Map m; + final Map m; try { - m = (Map) mapClass.newInstance(); + m = (Map) mapClass.getConstructor().newInstance(); } catch (Exception e) { throw new RuntimeException("Can't instantiate " + mapClass + ": " + e); } @@ -334,6 +333,12 @@ public class MapMicroBenchmark { if (m.put(x, x) == null) ++sum; } + /* // uncomment to avoid calling clear() + for (int i = 0; i < len; ++i) { + Object x = keys[i]; + m.remove(x); + } + */ m.clear(); sum += len - (quarter * 2); checkSum += sum ^ (sum << 12); @@ -357,7 +362,6 @@ public class MapMicroBenchmark { } - static final Random rng = new Random(3122688); // Shuffle the subarrays for each size. This doesn't fully @@ -377,7 +381,7 @@ public class MapMicroBenchmark { // plain array shuffle static void shuffle(Object[] a, int size) { - for (int i= size; i>1; i--) { + for (int i = size; i > 1; i--) { Object t = a[i-1]; int r = rng.nextInt(i); a[i-1] = a[r]; @@ -438,7 +442,7 @@ public class MapMicroBenchmark { BufferedInputStream in = new BufferedInputStream(fr); int k = 0; - outer:while (k < n) { + outer: while (k < n) { StringBuffer sb = new StringBuffer(); for (;;) { int c = in.read();