--- jsr166/src/test/loops/CollectionLoops.java 2005/10/04 20:09:41 1.1 +++ jsr166/src/test/loops/CollectionLoops.java 2016/10/23 03:03:23 1.11 @@ -1,10 +1,9 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 - * Expert Group and released to the public domain. Use, modify, and - * redistribute this code in any way without acknowledgement. + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/publicdomain/zero/1.0/ */ - import java.util.*; import java.util.concurrent.*; @@ -23,34 +22,34 @@ public class CollectionLoops { int nkeys = 10000; int nops = 100000; - Class collectionClass = null; + Class collectionClass = null; if (args.length > 0) { try { collectionClass = Class.forName(args[0]); - } catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new RuntimeException("Class " + args[0] + " not found."); } } - if (args.length > 1) + if (args.length > 1) maxThreads = Integer.parseInt(args[1]); - if (args.length > 2) + if (args.length > 2) nkeys = Integer.parseInt(args[2]); - if (args.length > 3) + if (args.length > 3) pinsert = Integer.parseInt(args[3]); - if (args.length > 4) + if (args.length > 4) premove = Integer.parseInt(args[4]); - if (args.length > 5) + if (args.length > 5) nops = Integer.parseInt(args[5]); // normalize probabilities wrt random number generator removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); - + System.out.print("Class: " + collectionClass.getName()); System.out.print(" threads: " + maxThreads); System.out.print(" size: " + nkeys); @@ -65,9 +64,8 @@ public class CollectionLoops { test(4, 100, 100, collectionClass); print = true; - int k = 1; int warmups = 2; - for (int i = 1; i <= maxThreads;) { + for (int k = 1, i = 1; i <= maxThreads;) { Thread.sleep(100); test(i, nkeys, nops, collectionClass); if (warmups > 0) @@ -75,12 +73,12 @@ public class CollectionLoops { else if (i == k) { k = i << 1; i = i + (i >>> 1); - } + } else if (i == 1 && k == 2) { i = k; warmups = 1; } - else + else i = k; } pool.shutdown(); @@ -89,7 +87,7 @@ public class CollectionLoops { static Integer[] makeKeys(int n) { LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom(); Integer[] key = new Integer[n]; - for (int i = 0; i < key.length; ++i) + for (int i = 0; i < key.length; ++i) key[i] = new Integer(rng.next()); return key; } @@ -104,26 +102,27 @@ public class CollectionLoops { } } - static void test(int i, int nk, int nops, Class collectionClass) throws Exception { + static void test(int i, int nk, int nops, Class collectionClass) throws Exception { if (print) System.out.print("Threads: " + i + "\t:"); - Collection collection = (Collection)collectionClass.newInstance(); + Collection collection = + (Collection) collectionClass.getConstructor().newInstance(); Integer[] key = makeKeys(nk); // Uncomment to start with a non-empty table - for (int j = 0; j < nk; j += 2) + for (int j = 0; j < nk; j += 2) collection.add(key[j]); shuffleKeys(key); LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier barrier = new CyclicBarrier(i+1, timer); - for (int t = 0; t < i; ++t) + for (int t = 0; t < i; ++t) pool.execute(new Runner(t, collection, key, barrier, nops)); barrier.await(); barrier.await(); long time = timer.getTime(); - long tpo = time / (i * (long)nops); + long tpo = time / (i * (long) nops); if (print) System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op"); - double secs = (double)(time) / 1000000000.0; + double secs = (double) time / 1000000000.0; if (print) System.out.print("\t " + secs + "s run time"); if (checkSum == 0) System.out.print(" "); @@ -141,12 +140,12 @@ public class CollectionLoops { int total; int nops; - Runner(int id, Collection collection, Integer[] key, CyclicBarrier barrier, int nops) { - this.collection = collection; - this.key = key; + Runner(int id, Collection collection, Integer[] key, CyclicBarrier barrier, int nops) { + this.collection = collection; + this.key = key; this.barrier = barrier; this.nops = nops; - position = key.length / (id + 1); + position = key.length / (id + 1); rng = new LoopHelpers.SimpleRandom((id + 1) * 8862213513L); rng.next(); } @@ -160,14 +159,14 @@ public class CollectionLoops { while (ops > 0) { int r = rng.next(); p += (r & 7) - 3; - while (p >= key.length) p -= key.length; + while (p >= key.length) p -= key.length; while (p < 0) p += key.length; Integer k = key[p]; if (c.contains(k)) { if (r < removesPerMaxRandom) { if (c.remove(k)) { - p = Math.abs(total % key.length); + p = Math.abs(total % key.length); ops -= 2; continue; } @@ -178,8 +177,8 @@ public class CollectionLoops { ops -= 2; c.add(k); continue; - } - + } + total += LoopHelpers.compute6(k.intValue()); --ops; } @@ -192,4 +191,3 @@ public class CollectionLoops { } } } -