--- jsr166/src/test/loops/CollectionWordLoops.java 2005/08/07 19:25:55 1.2 +++ jsr166/src/test/loops/CollectionWordLoops.java 2015/08/10 03:13:33 1.11 @@ -1,19 +1,19 @@ /* * 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.util.*; public class CollectionWordLoops { static final String[] WORDS_FILES = { - "kw.txt", + "kw.txt", "class.txt", // "dir.txt", - // "ids.txt", - // "/usr/dict/words", + // "ids.txt", + // "/usr/dict/words", }; static final int MAX_WORDS = 500000; @@ -23,31 +23,30 @@ public class CollectionWordLoops { static final int numTests = 2; public static void main(String[] args) { - Class collectionClass = null; + Class collectionClass = null; try { collectionClass = Class.forName(args[0]); - } catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new RuntimeException("Class " + args[0] + " not found."); } System.out.println("Testing " + collectionClass.getName()); - for (int s = 0; s < WORDS_FILES.length; ++s) + for (int s = 0; s < WORDS_FILES.length; ++s) tests(collectionClass, numTests, s); - for (int s = WORDS_FILES.length-1; s >= 0; --s) + for (int s = WORDS_FILES.length-1; s >= 0; --s) tests(collectionClass, numTests, s); - } - static void tests(Class collectionClass, int numTests, int sizeIndex) { - try { + static void tests(Class collectionClass, 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) { Collection m = newCollection(collectionClass); long t = doTest(collectionClass.getName(), m, key); @@ -55,7 +54,7 @@ public class CollectionWordLoops { m.clear(); m = null; } - + long nano = Math.round(1000000.0 * (least) / NOPS); System.out.println(LoopHelpers.rightJustify(nano) + " ns per op"); } catch (IOException ignore) { @@ -63,18 +62,18 @@ public class CollectionWordLoops { } } - - static Collection newCollection(Class cl) { + static Collection newCollection(Class cl) { try { - Collection m = (Collection)cl.newInstance(); + Collection m = (Collection) 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 { @@ -105,7 +104,7 @@ public class CollectionWordLoops { } static long doTest(String name, - final Collection m, + final Collection m, final String[] key) { // System.out.print(name + "\t"); @@ -113,18 +112,17 @@ public class CollectionWordLoops { 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 Collection collection; final String[] key; @@ -139,25 +137,24 @@ public class CollectionWordLoops { int maxsz; Runner(Collection m, String[] k) { - collection = m; key = k; + collection = m; key = k; pctrem = (int)(((long)premove * (long)(Integer.MAX_VALUE/2)) / 50); pctins = (int)(((long)pinsert * (long)(Integer.MAX_VALUE/2)) / 50); } - 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]; - + if (!collection.contains(k)) { ++nagets; if (r < pctins) { @@ -172,7 +169,7 @@ public class CollectionWordLoops { if (r < pctrem) { collection.remove(k); ++nremoves; - j += ((r >>> 8) & 7) + n / 2; + j += ((r >>> 8) & 7) + n / 2; if (j >= n) j -= n; } }