--- jsr166/src/test/loops/IteratorLoops.java 2009/10/23 19:57:06 1.4 +++ jsr166/src/test/loops/IteratorLoops.java 2015/08/10 07:24:02 1.13 @@ -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.*; @@ -14,7 +14,6 @@ import java.util.*; * and long iterators. Reports include times for adds and other * checks, so overestimate times per iteration. */ - public final class IteratorLoops { static final int DEFAULT_SIZE = 16384; static final int DEFAULT_TRIALS = 4; @@ -23,24 +22,25 @@ public final class IteratorLoops { static int randomSeed = 3122688; public static void main(String[] args) throws Exception { - Class klass = Class.forName(args[0]); - int n = (args.length <= 1)? DEFAULT_SIZE : Integer.parseInt(args[1]); - int t = (args.length <= 2)? DEFAULT_TRIALS : Integer.parseInt(args[2]); + Class klass = Class.forName(args[0]); + int n = (args.length <= 1) ? DEFAULT_SIZE : Integer.parseInt(args[1]); + int t = (args.length <= 2) ? DEFAULT_TRIALS : Integer.parseInt(args[2]); System.out.print("Class: " + klass.getName()); - System.out.print(" ~iters: " + (long)n * (long)n); + System.out.print(" ~iters: " + (long) n * (long) n); System.out.print(" trials: " + t); System.out.println(); Collection[] colls = - (Collection[])new Collection[NC]; + (Collection[])new Collection[NC]; for (int k = 0; k < colls.length; ++k) { Object x = klass.newInstance(); if (x instanceof Collection) - colls[k] = (Collection)x; + colls[k] = (Collection) x; else if (x instanceof Map) - colls[k] = (Collection)Collections.newSetFromMap((Map)x); + colls[k] = (Collection) + Collections.newSetFromMap((Map) x); else throw new Error("bad class"); } @@ -64,7 +64,7 @@ public final class IteratorLoops { preload(n); long startTime = System.nanoTime(); long count = traversals(n); - double elapsed = (double)(System.nanoTime() - startTime); + double elapsed = (double) (System.nanoTime() - startTime); double npi = elapsed / count; double secs = elapsed / 1000000000; System.out.printf("%7.1f ns/iter %8.3fs run time\n", npi, secs); @@ -73,7 +73,7 @@ public final class IteratorLoops { long traversals(int n) { long count = 0; long check = 0; - for (int i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { check += elementCount; count += counts(); maybeAdd(); @@ -86,8 +86,8 @@ public final class IteratorLoops { int counts() { int count = 0; for (int k = 0; k < cs.length; ++k) { - for (Iterator it = cs[k].iterator(); it.hasNext();) { - if (it.next() != null) + for (Integer x : cs[k]) { + if (x != null) ++count; } } @@ -119,8 +119,7 @@ public final class IteratorLoops { k >>>= 1; } // let GC settle down - try { Thread.sleep(500); } catch(Exception ex) { return; } + try { Thread.sleep(500); } + catch (Exception ex) { return; } } - - }