--- jsr166/src/test/loops/MapMicroBenchmark.java 2009/07/26 15:40:47 1.3 +++ jsr166/src/test/loops/MapMicroBenchmark.java 2011/07/15 13:21:33 1.11 @@ -1,7 +1,7 @@ /* * 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.*; @@ -11,7 +11,7 @@ import java.math.*; /** * A micro-benchmark with key types and operation mixes roughly * corresponding to some real programs. - * + * * The main results are a table of approximate nanoseconds per * element-operation (averaged across get, put etc) for each type, * across a range of map sizes. It also includes category "Mixed" @@ -24,14 +24,14 @@ import java.math.*; * dynamic type specialization. Some test classes, like Float and * BigDecimal are included not because they are commonly used as keys, * but because they can be problematic for some map implementations. - * + * * By default, it creates and inserts in order dense numerical keys * and searches for keys in scrambled order. Use "s" as second arg to * instead insert and search in unscrambled order. * * For String keys, the program tries to use file "testwords.txt", which * is best used with real words. We can't check in this file, but you - * can create one from a real dictonary (1 line per word) and then run + * can create one from a real dictionary (1 line per word) and then run * linux "shuf" to randomize entries. If no file exists, it uses * String.valueOf(i) for element i. */ @@ -61,7 +61,7 @@ public class MapMicroBenchmark { System.out.println("Usage: java MapMicroBenchmark className [r|s]keys [r|s]searches"); return; } - + mapClass = Class.forName(args[0]); if (args.length > 1) { @@ -102,15 +102,15 @@ public class MapMicroBenchmark { jobs[2] = new Job("Integer ", is, Integer.class); Object[] ls = new Object[n]; - for (int i = 0; i < n; i++) ls[i] = Long.valueOf((long)i); + for (int i = 0; i < n; i++) ls[i] = Long.valueOf((long) i); jobs[3] = new Job("Long ", ls, Long.class); Object[] fs = new Object[n]; - for (int i = 0; i < n; i++) fs[i] = Float.valueOf((float)i); + for (int i = 0; i < n; i++) fs[i] = Float.valueOf((float) i); jobs[4] = new Job("Float ", fs, Float.class); Object[] ds = new Object[n]; - for (int i = 0; i < n; i++) ds[i] = Double.valueOf((double)i); + for (int i = 0; i < n; i++) ds[i] = Double.valueOf((double) i); jobs[5] = new Job("Double ", ds, Double.class); Object[] bs = new Object[n]; @@ -143,7 +143,7 @@ public class MapMicroBenchmark { warmup1(mixed); warmup3(jobs); Thread.sleep(500); - time(jobs); + time(jobs); } static void runWork(Job[] jobs, int minIters, int maxIters, long timeLimit) throws Throwable { @@ -193,7 +193,7 @@ public class MapMicroBenchmark { long[] aves = new long[nsizes]; int njobs = jobs.length; - for (int i = 0; i < njobs; i++) { + for (int i = 0; i < njobs; i++) { System.out.print(jobs[i].name); for (int k = 0; k < nsizes; ++k) { long nanos = jobs[i].nanos[k]; @@ -212,7 +212,7 @@ public class MapMicroBenchmark { static final class Job { - final String name; + final String name; final Class elementClass; long[] nanos = new long[nsizes]; final Object[] items; @@ -236,8 +236,8 @@ public class MapMicroBenchmark { public long work(int len, int minIters, int maxIters, long timeLimit) { Map m; try { - m = (Map)mapClass.newInstance(); - } catch(Exception e) { + m = (Map) mapClass.newInstance(); + } catch (Exception e) { throw new RuntimeException("Can't instantiate " + mapClass + ": " + e); } Object[] ins = items; @@ -261,7 +261,7 @@ public class MapMicroBenchmark { sum += len - half; for (int i = 0; i < len; ++i) { Object x = keys[i]; - Object v = m.get(x); + Object v = m.get(x); if (elementClass.isInstance(v)) // touch v ++sum; } @@ -273,12 +273,12 @@ public class MapMicroBenchmark { } checkSum += sum ^ (sum << 3); for (Object e : m.keySet()) { - if (elementClass.isInstance(e)) + if (elementClass.isInstance(e)) ++sum; } checkSum += sum ^ (sum << 4); for (Object e : m.values()) { - if (elementClass.isInstance(e)) + if (elementClass.isInstance(e)) ++sum; } checkSum += sum ^ (sum << 5); @@ -334,23 +334,29 @@ 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); if (j == 0 && sum != lastSum + len * OPS_PER_ITER) throw new Error(name); - + elapsed = System.nanoTime() - startTime; ++j; if (j >= minIters && (j >= maxIters || elapsed >= timeLimit)) break; // non-warmup - swap some keys for next insert - if (minIters != 1 && randomSearches) + if (minIters != 1 && randomSearches) shuffleSome(ins, len, len >>> 3); } - long ops = ((long)j) * len * OPS_PER_ITER; + long ops = ((long) j) * len * OPS_PER_ITER; lastSum = sum; return elapsed / ops; } @@ -365,7 +371,7 @@ public class MapMicroBenchmark { // more realistic static void scramble(Object[] a) { for (int k = 0; k < sizes.length; ++k) { - int origin = k == 0? 0 : sizes[k-1]; + int origin = (k == 0) ? 0 : sizes[k-1]; for (int i = sizes[k]; i > origin + 1; i--) { Object t = a[i-1]; int r = rng.nextInt(i - origin) + origin; @@ -373,7 +379,7 @@ public class MapMicroBenchmark { a[r] = t; } } - } + } // plain array shuffle static void shuffle(Object[] a, int size) { @@ -425,7 +431,7 @@ public class MapMicroBenchmark { } // Read in String keys from file if possible - static void initStringKeys(Object[] keys, int n) throws Exception { + static void initStringKeys(Object[] keys, int n) throws Exception { FileInputStream fr = null; try { fr = new FileInputStream(wordFile); @@ -442,9 +448,9 @@ public class MapMicroBenchmark { StringBuffer sb = new StringBuffer(); for (;;) { int c = in.read(); - if (c < 0) + if (c < 0) break outer; - char ch = (char)c; + char ch = (char) c; if (ch == '\n') { keys[k++] = sb.toString(); break; @@ -458,8 +464,7 @@ public class MapMicroBenchmark { // fill up remaining keys with path-like compounds of previous pairs int j = 0; while (k < n) - keys[k++] = (String)keys[j++] + "/" + (String)keys[j]; + keys[k++] = (String) keys[j++] + "/" + (String) keys[j]; } } -