1 |
dl |
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 |
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 |
jsr166 |
1.2 |
do { |
38 |
|
|
j++; |
39 |
|
|
jobs[i].work(); |
40 |
dl |
1.1 |
} 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 |
jsr166 |
1.4 |
double ratio = (double) jobs[i].nanos / (double) jobs[0].nanos; |
69 |
dl |
1.1 |
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; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
static String[] toStrings(Integer[] ints) { |
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; |
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; |
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; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
|
104 |
|
|
static final class Hasher extends Job { |
105 |
|
|
final Object[] elts; |
106 |
|
|
final Class mapClass; |
107 |
|
|
volatile int matches; |
108 |
|
|
Hasher(String name, Object[] elts, Class mapClass) { |
109 |
|
|
super(name); |
110 |
|
|
this.elts = elts; |
111 |
|
|
this.mapClass = mapClass; |
112 |
|
|
} |
113 |
|
|
public void work() { |
114 |
|
|
Map m = null; |
115 |
|
|
try { |
116 |
jsr166 |
1.3 |
m = (Map) mapClass.newInstance(); |
117 |
|
|
} catch (Exception e) { |
118 |
dl |
1.1 |
throw new RuntimeException("Can't instantiate " + mapClass + ": " + e); |
119 |
|
|
} |
120 |
|
|
final int len = elts.length; |
121 |
|
|
for (int j = 0; j < ITERS_PER_TEST; j++) { |
122 |
|
|
for (Object x : elts) { |
123 |
|
|
if (m.put(x, x) != null) |
124 |
|
|
throw new Error(); |
125 |
|
|
} |
126 |
|
|
if (m.size() != len) |
127 |
|
|
throw new Error(); |
128 |
|
|
int ng = 0; |
129 |
|
|
for (Object x : elts) { |
130 |
|
|
if (m.get(x) == x) |
131 |
|
|
++ng; |
132 |
|
|
} |
133 |
|
|
matches += ng; |
134 |
|
|
for (Object e : m.keySet()) { |
135 |
|
|
if (m.get(e) == e) |
136 |
|
|
--ng; |
137 |
|
|
} |
138 |
|
|
if (ng != 0) |
139 |
|
|
throw new Error(); |
140 |
|
|
for (Object x : elts) { |
141 |
|
|
if (m.remove(x) != x) |
142 |
|
|
throw new Error(); |
143 |
|
|
} |
144 |
|
|
if (!m.isEmpty()) |
145 |
|
|
throw new Error(); |
146 |
|
|
} |
147 |
|
|
if (matches != len * ITERS_PER_TEST) |
148 |
|
|
throw new Error(); |
149 |
|
|
matches = 0; |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
public static void main(String[] args) throws Throwable { |
154 |
|
|
Class mc = java.util.HashMap.class; |
155 |
|
|
if (args.length > 0) |
156 |
|
|
mc = Class.forName(args[0]); |
157 |
|
|
if (args.length > 1) |
158 |
|
|
SIZE = Integer.parseInt(args[1]); |
159 |
|
|
|
160 |
|
|
System.out.print("Class " + mc.getName()); |
161 |
|
|
System.out.print(" size " + SIZE); |
162 |
|
|
System.out.println(); |
163 |
|
|
|
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>(); |
170 |
|
|
hashers.add(new Hasher("Integer sequential", seq, mc)); |
171 |
|
|
hashers.add(new Hasher("Integer shuffled", shf, mc)); |
172 |
|
|
|
173 |
|
|
hashers.add(new Hasher("Long sequential", toLongs(seq), mc)); |
174 |
|
|
hashers.add(new Hasher("Long shuffled", toLongs(shf), mc)); |
175 |
|
|
|
176 |
|
|
hashers.add(new Hasher("Float sequential", toFloats(seq), mc)); |
177 |
|
|
hashers.add(new Hasher("Float shuffled", toFloats(shf), mc)); |
178 |
|
|
|
179 |
|
|
hashers.add(new Hasher("Double sequential", toDoubles(seq), mc)); |
180 |
|
|
hashers.add(new Hasher("Double shuffled", toDoubles(shf), mc)); |
181 |
|
|
|
182 |
|
|
hashers.add(new Hasher("String sequential", toStrings(seq), mc)); |
183 |
|
|
hashers.add(new Hasher("String shuffled", toStrings(shf), mc)); |
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 |
189 |
|
|
for (int i = 0; i < 2; i++) { |
190 |
|
|
System.gc(); |
191 |
|
|
Thread.sleep(50); |
192 |
|
|
System.runFinalization(); |
193 |
|
|
Thread.sleep(50); |
194 |
|
|
} |
195 |
|
|
System.out.println("starting"); |
196 |
|
|
time(jobs); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
} |