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.4 by jsr166, Tue Nov 3 01:04:02 2009 UTC vs.
Revision 1.5 by jsr166, Wed Sep 1 07:20:36 2010 UTC

# 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  
74      static Long[] toLongs(Integer[] ints) {
75 <        Long[] longs = new Long[ints.length];
76 <        for (int i = 0; i < ints.length; i++)
77 <            longs[i] = ints[i].longValue();
78 <        return longs;
75 >        Long[] longs = new Long[ints.length];
76 >        for (int i = 0; i < ints.length; i++)
77 >            longs[i] = ints[i].longValue();
78 >        return longs;
79      }
80  
81      static String[] toStrings(Integer[] ints) {
82 <        String[] strings = new String[ints.length];
83 <        for (int i = 0; i < ints.length; i++)
82 >        String[] strings = new String[ints.length];
83 >        for (int i = 0; i < ints.length; i++)
84              strings[i] = ints[i].toString();
85          //            strings[i] = String.valueOf(ints[i].doubleValue());
86 <        return strings;
86 >        return strings;
87      }
88  
89      static Float[] toFloats(Integer[] ints) {
90 <        Float[] floats = new Float[ints.length];
91 <        for (int i = 0; i < ints.length; i++)
92 <            floats[i] = ints[i].floatValue();
93 <        return floats;
90 >        Float[] floats = new Float[ints.length];
91 >        for (int i = 0; i < ints.length; i++)
92 >            floats[i] = ints[i].floatValue();
93 >        return floats;
94      }
95  
96      static Double[] toDoubles(Integer[] ints) {
97 <        Double[] doubles = new Double[ints.length];
98 <        for (int i = 0; i < ints.length; i++)
99 <            doubles[i] = ints[i].doubleValue();
100 <        return doubles;
97 >        Double[] doubles = new Double[ints.length];
98 >        for (int i = 0; i < ints.length; i++)
99 >            doubles[i] = ints[i].doubleValue();
100 >        return doubles;
101      }
102  
103  
# Line 161 | Line 161 | public class DenseMapMicroBenchmark {
161          System.out.print(" size " + SIZE);
162          System.out.println();
163  
164 <        final Integer[] seq = new Integer[SIZE];
164 >        final Integer[] seq = new Integer[SIZE];
165          for (int i = 0; i < SIZE; i++)
166              seq[i] = new Integer(i);
167 <        final Integer[] shf = seq.clone();
168 <        Collections.shuffle(Arrays.asList(shf));
169 <        List<Hasher> hashers = new ArrayList<Hasher>();
167 >        final Integer[] shf = seq.clone();
168 >        Collections.shuffle(Arrays.asList(shf));
169 >        List<Hasher> hashers = new ArrayList<Hasher>();
170          hashers.add(new Hasher("Integer sequential", seq, mc));
171          hashers.add(new Hasher("Integer shuffled", shf, mc));
172  
# Line 184 | Line 184 | public class DenseMapMicroBenchmark {
184  
185          Hasher[] jobs = hashers.toArray(new Hasher[0]);
186          System.out.print("warmup...");
187 <        time0(NANOS_PER_WARMUP, jobs); // Warm up run
188 <        time0(NANOS_PER_WARMUP, jobs); // Warm up run
187 >        time0(NANOS_PER_WARMUP, jobs); // Warm up run
188 >        time0(NANOS_PER_WARMUP, jobs); // Warm up run
189          for (int i = 0; i < 2; i++) {
190              System.gc();
191              Thread.sleep(50);
# Line 193 | Line 193 | public class DenseMapMicroBenchmark {
193              Thread.sleep(50);
194          }
195          System.out.println("starting");
196 <        time(jobs);
196 >        time(jobs);
197      }
198  
199   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines