--- jsr166/src/test/loops/DenseMapMicroBenchmark.java 2009/11/03 01:04:02 1.4 +++ jsr166/src/test/loops/DenseMapMicroBenchmark.java 2010/09/01 07:20:36 1.5 @@ -16,12 +16,12 @@ public class DenseMapMicroBenchmark { static int SIZE = 50000; // may be replaced by program arg abstract static class Job { - final String name; + final String name; long nanos; int runs; - public Job(String name) { this.name = name; } - public String name() { return name; } - public abstract void work() throws Throwable; + public Job(String name) { this.name = name; } + public String name() { return name; } + public abstract void work() throws Throwable; } /** @@ -29,75 +29,75 @@ public class DenseMapMicroBenchmark { * Returns array of average times per job per run. */ static void time0(long nanos, Job ... jobs) throws Throwable { - for (int i = 0; i < jobs.length; i++) { + for (int i = 0; i < jobs.length; i++) { Thread.sleep(50); - long t0 = System.nanoTime(); - long t; - int j = 0; - do { + long t0 = System.nanoTime(); + long t; + int j = 0; + do { j++; jobs[i].work(); } while ((t = System.nanoTime() - t0) < nanos); jobs[i].nanos = t / j; jobs[i].runs = j; - } + } } static void time(Job ... jobs) throws Throwable { time0(NANOS_PER_JOB, jobs); - final String nameHeader = "Method"; - int nameWidth = nameHeader.length(); - for (Job job : jobs) - nameWidth = Math.max(nameWidth, job.name().length()); + final String nameHeader = "Method"; + int nameWidth = nameHeader.length(); + for (Job job : jobs) + nameWidth = Math.max(nameWidth, job.name().length()); final int itemsPerTest = SIZE * OPS_PER_ITER * ITERS_PER_TEST; - final String timeHeader = "Nanos/item"; - int timeWidth = timeHeader.length(); - final String ratioHeader = "Ratio"; - int ratioWidth = ratioHeader.length(); - String format = String.format("%%-%ds %%%dd %%.3f%%n", - nameWidth, timeWidth); - String headerFormat = String.format("%%-%ds %%-%ds %%-%ds%%n", - nameWidth, timeWidth, ratioWidth); - System.out.printf(headerFormat, "Method", "Nanos/item", "Ratio"); - - // Print out absolute and relative times, calibrated against first job - for (int i = 0; i < jobs.length; i++) { - long time = jobs[i].nanos/itemsPerTest; - double ratio = (double) jobs[i].nanos / (double) jobs[0].nanos; - System.out.printf(format, jobs[i].name(), time, ratio); - } + final String timeHeader = "Nanos/item"; + int timeWidth = timeHeader.length(); + final String ratioHeader = "Ratio"; + int ratioWidth = ratioHeader.length(); + String format = String.format("%%-%ds %%%dd %%.3f%%n", + nameWidth, timeWidth); + String headerFormat = String.format("%%-%ds %%-%ds %%-%ds%%n", + nameWidth, timeWidth, ratioWidth); + System.out.printf(headerFormat, "Method", "Nanos/item", "Ratio"); + + // Print out absolute and relative times, calibrated against first job + for (int i = 0; i < jobs.length; i++) { + long time = jobs[i].nanos/itemsPerTest; + double ratio = (double) jobs[i].nanos / (double) jobs[0].nanos; + System.out.printf(format, jobs[i].name(), time, ratio); + } } static Long[] toLongs(Integer[] ints) { - Long[] longs = new Long[ints.length]; - for (int i = 0; i < ints.length; i++) - longs[i] = ints[i].longValue(); - return longs; + Long[] longs = new Long[ints.length]; + for (int i = 0; i < ints.length; i++) + longs[i] = ints[i].longValue(); + return longs; } static String[] toStrings(Integer[] ints) { - String[] strings = new String[ints.length]; - for (int i = 0; i < ints.length; i++) + String[] strings = new String[ints.length]; + for (int i = 0; i < ints.length; i++) strings[i] = ints[i].toString(); // strings[i] = String.valueOf(ints[i].doubleValue()); - return strings; + return strings; } static Float[] toFloats(Integer[] ints) { - Float[] floats = new Float[ints.length]; - for (int i = 0; i < ints.length; i++) - floats[i] = ints[i].floatValue(); - return floats; + Float[] floats = new Float[ints.length]; + for (int i = 0; i < ints.length; i++) + floats[i] = ints[i].floatValue(); + return floats; } static Double[] toDoubles(Integer[] ints) { - Double[] doubles = new Double[ints.length]; - for (int i = 0; i < ints.length; i++) - doubles[i] = ints[i].doubleValue(); - return doubles; + Double[] doubles = new Double[ints.length]; + for (int i = 0; i < ints.length; i++) + doubles[i] = ints[i].doubleValue(); + return doubles; } @@ -161,12 +161,12 @@ public class DenseMapMicroBenchmark { System.out.print(" size " + SIZE); System.out.println(); - final Integer[] seq = new Integer[SIZE]; + final Integer[] seq = new Integer[SIZE]; for (int i = 0; i < SIZE; i++) seq[i] = new Integer(i); - final Integer[] shf = seq.clone(); - Collections.shuffle(Arrays.asList(shf)); - List hashers = new ArrayList(); + final Integer[] shf = seq.clone(); + Collections.shuffle(Arrays.asList(shf)); + List hashers = new ArrayList(); hashers.add(new Hasher("Integer sequential", seq, mc)); hashers.add(new Hasher("Integer shuffled", shf, mc)); @@ -184,8 +184,8 @@ public class DenseMapMicroBenchmark { Hasher[] jobs = hashers.toArray(new Hasher[0]); System.out.print("warmup..."); - time0(NANOS_PER_WARMUP, jobs); // Warm up run - time0(NANOS_PER_WARMUP, jobs); // Warm up run + time0(NANOS_PER_WARMUP, jobs); // Warm up run + time0(NANOS_PER_WARMUP, jobs); // Warm up run for (int i = 0; i < 2; i++) { System.gc(); Thread.sleep(50); @@ -193,7 +193,7 @@ public class DenseMapMicroBenchmark { Thread.sleep(50); } System.out.println("starting"); - time(jobs); + time(jobs); } }