--- jsr166/src/test/loops/MapWordLoops.java 2005/05/02 19:19:38 1.1 +++ jsr166/src/test/loops/MapWordLoops.java 2015/08/10 03:13:33 1.12 @@ -1,56 +1,62 @@ -import java.util.*; +/* + * 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/publicdomain/zero/1.0/ + */ import java.io.*; +import java.util.*; public class MapWordLoops { static final String[] WORDS_FILES = { - "kw.txt", + "kw.txt", "class.txt", "dir.txt", - "ids.txt", - "/usr/dict/words", + "ids.txt", + "testwords.txt", + // "/usr/dict/words", }; static final int MAX_WORDS = 500000; static final int pinsert = 60; - static final int premove = 1; - static final int NOPS = 5000000; + static final int premove = 2; + static final int NOPS = 8000000; + static final int numTests = 3; public static void main(String[] args) { - Class mapClass = null; + Class mapClass = null; try { mapClass = Class.forName(args[0]); - } catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new RuntimeException("Class " + args[0] + " not found."); } System.out.println("Testing " + mapClass.getName()); - for (int s = 0; s < WORDS_FILES.length; ++s) + for (int s = 0; s < WORDS_FILES.length; ++s) tests(mapClass, numTests, s); - for (int s = WORDS_FILES.length-1; s >= 0; --s) + for (int s = WORDS_FILES.length-1; s >= 0; --s) tests(mapClass, numTests, s); - } - static void tests(Class mapClass, int numTests, int sizeIndex) { - try { + static void tests(Class mapClass, int numTests, int sizeIndex) { + try { String[] key = readWords(sizeIndex); int size = key.length; - + System.out.print("n = " +LoopHelpers.rightJustify(size) +" : "); long least = Long.MAX_VALUE; - + for (int i = 0; i < numTests; ++i) { Map m = newMap(mapClass); - long t = doTest(mapClass.getName(), m, key); + long t = doTest(i, mapClass.getName(), m, key); if (t < least) least = t; m.clear(); m = null; } - + long nano = Math.round(1000000.0 * (least) / NOPS); System.out.println(LoopHelpers.rightJustify(nano) + " ns per op"); } catch (IOException ignore) { @@ -58,18 +64,18 @@ public class MapWordLoops { } } - - static Map newMap(Class cl) { + static Map newMap(Class cl) { try { Map m = (Map)cl.newInstance(); return m; - } catch(Exception e) { + } catch (Exception e) { throw new RuntimeException("Can't instantiate " + cl + ": " + e); } } static void pause() { - try { Thread.sleep(100); } catch(InterruptedException ie) { return; } + try { Thread.sleep(100); } + catch (InterruptedException ie) { return; } } static String[] readWords(int sizeIndex) throws IOException { @@ -99,31 +105,30 @@ public class MapWordLoops { return array; } - static long doTest(String name, - final Map m, + static long doTest(int id, String name, + final Map m, final String[] key) { // System.out.print(name + "\t"); - Runner runner = new Runner(m, key); + Runner runner = new Runner(id, m, key); long startTime = System.currentTimeMillis(); runner.run(); long afterRun = System.currentTimeMillis(); - long runTime = (afterRun - startTime); + long runTime = afterRun - startTime; int np = runner.total; - if (runner.total == runner.hashCode()) + if (runner.total == runner.hashCode()) System.out.println("Useless Number" + runner.total); int sz = runner.maxsz; - if (sz == runner.hashCode()) + if (sz == runner.hashCode()) System.out.println("Useless Number" + sz); // System.out.print(" m = " + sz); return runTime; } - static class Runner implements Runnable { final Map map; final String[] key; - LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); + LoopHelpers.SimpleRandom rng; final int pctrem; final int pctins; int nputs = 0; @@ -133,27 +138,27 @@ public class MapWordLoops { volatile int total; int maxsz; - Runner(Map m, String[] k) { - map = m; key = k; + Runner(int id, Map m, String[] k) { + map = m; key = k; pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50); pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50); + rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); } - int oneStep(int j) { int n = key.length; int r = rng.next() & 0x7FFFFFFF; int jinc = (r & 7); j += jinc - 3; - if (j >= n) j -= n; + if (j >= n) j -= n; if (j < 0) j += n; int l = n / 4 + j; - if (l >= n) l -= n; - + if (l >= n) l -= n; + String k = key[j]; String x = map.get(k); - + if (x == null) { ++nagets; if (r < pctins) { @@ -168,7 +173,7 @@ public class MapWordLoops { if (r < pctrem) { map.remove(k); ++nremoves; - j += ((r >>> 8) & 7) + n / 2; + j += ((r >>> 8) & 7) + n / 2; if (j >= n) j -= n; } }