ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/DenseMapMicroBenchmark.java
(Generate patch)

Comparing jsr166/src/test/loops/DenseMapMicroBenchmark.java (file contents):
Revision 1.2 by jsr166, Thu Oct 29 23:09:07 2009 UTC vs.
Revision 1.9 by jsr166, Thu Jan 15 18:34:19 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import java.util.*;
# Line 16 | Line 16 | public class DenseMapMicroBenchmark {
16      static int SIZE = 50000; // may be replaced by program arg
17  
18      abstract static class Job {
19 <        final String name;
19 >        final String name;
20          long nanos;
21          int runs;
22 <        public Job(String name) { this.name = name; }
23 <        public String name() { return name; }
24 <        public abstract void work() throws Throwable;
22 >        public Job(String name) { this.name = name; }
23 >        public String name() { return name; }
24 >        public abstract void work() throws Throwable;
25      }
26  
27      /**
# Line 29 | Line 29 | public class DenseMapMicroBenchmark {
29       * Returns array of average times per job per run.
30       */
31      static void time0(long nanos, Job ... jobs) throws Throwable {
32 <        for (int i = 0; i < jobs.length; i++) {
32 >        for (int i = 0; i < jobs.length; i++) {
33              Thread.sleep(50);
34 <            long t0 = System.nanoTime();
35 <            long t;
36 <            int j = 0;
37 <            do {
34 >            long t0 = System.nanoTime();
35 >            long t;
36 >            int j = 0;
37 >            do {
38                  j++;
39                  jobs[i].work();
40              } while ((t = System.nanoTime() - t0) < nanos);
41              jobs[i].nanos = t / j;
42              jobs[i].runs = j;
43 <        }
43 >        }
44      }
45  
46      static void time(Job ... jobs) throws Throwable {
47          time0(NANOS_PER_JOB, jobs);
48  
49 <        final String nameHeader = "Method";
50 <        int nameWidth  = nameHeader.length();
51 <        for (Job job : jobs)
52 <            nameWidth = Math.max(nameWidth, job.name().length());
49 >        final String nameHeader = "Method";
50 >        int nameWidth = nameHeader.length();
51 >        for (Job job : jobs)
52 >            nameWidth = Math.max(nameWidth, job.name().length());
53  
54          final int itemsPerTest = SIZE * OPS_PER_ITER * ITERS_PER_TEST;
55 <        final String timeHeader = "Nanos/item";
56 <        int timeWidth  = timeHeader.length();
57 <        final String ratioHeader = "Ratio";
58 <        int ratioWidth = ratioHeader.length();
59 <        String format = String.format("%%-%ds %%%dd %%.3f%%n",
60 <                                      nameWidth, timeWidth);
61 <        String headerFormat = String.format("%%-%ds %%-%ds %%-%ds%%n",
62 <                                            nameWidth, timeWidth, ratioWidth);
63 <        System.out.printf(headerFormat, "Method", "Nanos/item", "Ratio");
64 <
65 <        // Print out absolute and relative times, calibrated against first job
66 <        for (int i = 0; i < jobs.length; i++) {
67 <            long time = jobs[i].nanos/itemsPerTest;
68 <            double ratio = (double)jobs[i].nanos / (double)jobs[0].nanos;
69 <            System.out.printf(format, jobs[i].name(), time, ratio);
70 <        }
55 >        final String timeHeader = "Nanos/item";
56 >        int timeWidth = timeHeader.length();
57 >        final String ratioHeader = "Ratio";
58 >        int ratioWidth = ratioHeader.length();
59 >        String format = String.format("%%-%ds %%%dd %%.3f%%n",
60 >                                      nameWidth, timeWidth);
61 >        String headerFormat = String.format("%%-%ds %%-%ds %%-%ds%%n",
62 >                                            nameWidth, timeWidth, ratioWidth);
63 >        System.out.printf(headerFormat, "Method", "Nanos/item", "Ratio");
64 >
65 >        // Print out absolute and relative times, calibrated against first job
66 >        for (int i = 0; i < jobs.length; i++) {
67 >            long time = jobs[i].nanos/itemsPerTest;
68 >            double ratio = (double) jobs[i].nanos / (double) jobs[0].nanos;
69 >            System.out.printf(format, jobs[i].name(), time, ratio);
70 >        }
71      }
72  
73
73      static Long[] toLongs(Integer[] ints) {
74 <        Long[] longs = new Long[ints.length];
75 <        for (int i = 0; i < ints.length; i++)
76 <            longs[i] = ints[i].longValue();
77 <        return longs;
74 >        Long[] longs = new Long[ints.length];
75 >        for (int i = 0; i < ints.length; i++)
76 >            longs[i] = ints[i].longValue();
77 >        return longs;
78      }
79  
80      static String[] toStrings(Integer[] ints) {
81 <        String[] strings = new String[ints.length];
82 <        for (int i = 0; i < ints.length; i++)
81 >        String[] strings = new String[ints.length];
82 >        for (int i = 0; i < ints.length; i++)
83              strings[i] = ints[i].toString();
84          //            strings[i] = String.valueOf(ints[i].doubleValue());
85 <        return strings;
85 >        return strings;
86      }
87  
88      static Float[] toFloats(Integer[] ints) {
89 <        Float[] floats = new Float[ints.length];
90 <        for (int i = 0; i < ints.length; i++)
91 <            floats[i] = ints[i].floatValue();
92 <        return floats;
89 >        Float[] floats = new Float[ints.length];
90 >        for (int i = 0; i < ints.length; i++)
91 >            floats[i] = ints[i].floatValue();
92 >        return floats;
93      }
94  
95      static Double[] toDoubles(Integer[] ints) {
96 <        Double[] doubles = new Double[ints.length];
97 <        for (int i = 0; i < ints.length; i++)
98 <            doubles[i] = ints[i].doubleValue();
99 <        return doubles;
96 >        Double[] doubles = new Double[ints.length];
97 >        for (int i = 0; i < ints.length; i++)
98 >            doubles[i] = ints[i].doubleValue();
99 >        return doubles;
100      }
101  
103
102      static final class Hasher extends Job {
103          final Object[] elts;
104 <        final Class mapClass;
104 >        final Class<?> mapClass;
105          volatile int matches;
106 <        Hasher(String name, Object[] elts, Class mapClass) {
106 >        Hasher(String name, Object[] elts, Class<?> mapClass) {
107              super(name);
108              this.elts = elts;
109              this.mapClass = mapClass;
# Line 113 | Line 111 | public class DenseMapMicroBenchmark {
111          public void work() {
112              Map m = null;
113              try {
114 <                m = (Map)mapClass.newInstance();
115 <            } catch(Exception e) {
114 >                m = (Map) mapClass.newInstance();
115 >            } catch (Exception e) {
116                  throw new RuntimeException("Can't instantiate " + mapClass + ": " + e);
117              }
118              final int len = elts.length;
# Line 151 | Line 149 | public class DenseMapMicroBenchmark {
149      }
150  
151      public static void main(String[] args) throws Throwable {
152 <        Class mc = java.util.HashMap.class;
152 >        Class<?> mc = java.util.HashMap.class;
153          if (args.length > 0)
154              mc = Class.forName(args[0]);
155          if (args.length > 1)
# Line 161 | Line 159 | public class DenseMapMicroBenchmark {
159          System.out.print(" size " + SIZE);
160          System.out.println();
161  
162 <        final Integer[] seq = new Integer[SIZE];
162 >        final Integer[] seq = new Integer[SIZE];
163          for (int i = 0; i < SIZE; i++)
164              seq[i] = new Integer(i);
165 <        final Integer[] shf = seq.clone();
166 <        Collections.shuffle(Arrays.asList(shf));
167 <        List<Hasher> hashers = new ArrayList<Hasher>();
165 >        final Integer[] shf = seq.clone();
166 >        Collections.shuffle(Arrays.asList(shf));
167 >        List<Hasher> hashers = new ArrayList<Hasher>();
168          hashers.add(new Hasher("Integer sequential", seq, mc));
169          hashers.add(new Hasher("Integer shuffled", shf, mc));
170  
# Line 184 | Line 182 | public class DenseMapMicroBenchmark {
182  
183          Hasher[] jobs = hashers.toArray(new Hasher[0]);
184          System.out.print("warmup...");
185 <        time0(NANOS_PER_WARMUP, jobs); // Warm up run
186 <        time0(NANOS_PER_WARMUP, jobs); // Warm up run
185 >        time0(NANOS_PER_WARMUP, jobs); // Warm up run
186 >        time0(NANOS_PER_WARMUP, jobs); // Warm up run
187          for (int i = 0; i < 2; i++) {
188              System.gc();
189              Thread.sleep(50);
# Line 193 | Line 191 | public class DenseMapMicroBenchmark {
191              Thread.sleep(50);
192          }
193          System.out.println("starting");
194 <        time(jobs);
194 >        time(jobs);
195      }
196  
197   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines