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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
import java.util.*; |
8 |
|
9 |
public class DenseMapMicroBenchmark { |
10 |
static final int ITERS_PER_TEST = 4; |
11 |
static final long NANOS_PER_JOB = 4L * 1000L*1000L*1000L; // 4 sec |
12 |
static final long NANOS_PER_WARMUP = 500L*1000L*1000L; // 0.5 sec |
13 |
// map operations per iteration -- change if hasher.work changed |
14 |
static final int OPS_PER_ITER = 4; // put + get + iter + remove |
15 |
|
16 |
static int SIZE = 50000; // may be replaced by program arg |
17 |
|
18 |
abstract static class Job { |
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; |
25 |
} |
26 |
|
27 |
/** |
28 |
* Runs each job for at least NANOS_PER_JOB seconds. |
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++) { |
33 |
Thread.sleep(50); |
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 |
} |
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()); |
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 |
} |
71 |
} |
72 |
|
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; |
78 |
} |
79 |
|
80 |
static String[] toStrings(Integer[] ints) { |
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; |
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; |
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; |
100 |
} |
101 |
|
102 |
static final class Hasher extends Job { |
103 |
final Object[] elts; |
104 |
final Class<?> mapClass; |
105 |
volatile int matches; |
106 |
Hasher(String name, Object[] elts, Class<?> mapClass) { |
107 |
super(name); |
108 |
this.elts = elts; |
109 |
this.mapClass = mapClass; |
110 |
} |
111 |
public void work() { |
112 |
final Map m; |
113 |
try { |
114 |
m = (Map) mapClass.getConstructor().newInstance(); |
115 |
} catch (Exception e) { |
116 |
throw new RuntimeException("Can't instantiate " + mapClass + ": " + e); |
117 |
} |
118 |
final int len = elts.length; |
119 |
for (int j = 0; j < ITERS_PER_TEST; j++) { |
120 |
for (Object x : elts) { |
121 |
if (m.put(x, x) != null) |
122 |
throw new Error(); |
123 |
} |
124 |
if (m.size() != len) |
125 |
throw new Error(); |
126 |
int ng = 0; |
127 |
for (Object x : elts) { |
128 |
if (m.get(x) == x) |
129 |
++ng; |
130 |
} |
131 |
matches += ng; |
132 |
for (Object e : m.keySet()) { |
133 |
if (m.get(e) == e) |
134 |
--ng; |
135 |
} |
136 |
if (ng != 0) |
137 |
throw new Error(); |
138 |
for (Object x : elts) { |
139 |
if (m.remove(x) != x) |
140 |
throw new Error(); |
141 |
} |
142 |
if (!m.isEmpty()) |
143 |
throw new Error(); |
144 |
} |
145 |
if (matches != len * ITERS_PER_TEST) |
146 |
throw new Error(); |
147 |
matches = 0; |
148 |
} |
149 |
} |
150 |
|
151 |
public static void main(String[] args) throws Throwable { |
152 |
Class<?> mc = java.util.HashMap.class; |
153 |
if (args.length > 0) |
154 |
mc = Class.forName(args[0]); |
155 |
if (args.length > 1) |
156 |
SIZE = Integer.parseInt(args[1]); |
157 |
|
158 |
System.out.print("Class " + mc.getName()); |
159 |
System.out.print(" size " + SIZE); |
160 |
System.out.println(); |
161 |
|
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>(); |
168 |
hashers.add(new Hasher("Integer sequential", seq, mc)); |
169 |
hashers.add(new Hasher("Integer shuffled", shf, mc)); |
170 |
|
171 |
hashers.add(new Hasher("Long sequential", toLongs(seq), mc)); |
172 |
hashers.add(new Hasher("Long shuffled", toLongs(shf), mc)); |
173 |
|
174 |
hashers.add(new Hasher("Float sequential", toFloats(seq), mc)); |
175 |
hashers.add(new Hasher("Float shuffled", toFloats(shf), mc)); |
176 |
|
177 |
hashers.add(new Hasher("Double sequential", toDoubles(seq), mc)); |
178 |
hashers.add(new Hasher("Double shuffled", toDoubles(shf), mc)); |
179 |
|
180 |
hashers.add(new Hasher("String sequential", toStrings(seq), mc)); |
181 |
hashers.add(new Hasher("String shuffled", toStrings(shf), mc)); |
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 |
187 |
for (int i = 0; i < 2; i++) { |
188 |
System.gc(); |
189 |
Thread.sleep(50); |
190 |
System.runFinalization(); |
191 |
Thread.sleep(50); |
192 |
} |
193 |
System.out.println("starting"); |
194 |
time(jobs); |
195 |
} |
196 |
|
197 |
} |