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.*; |
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 |
|
/** |
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 |
|
|
104 |
|
static final class Hasher extends Job { |
105 |
|
final Object[] elts; |
106 |
< |
final Class mapClass; |
106 |
> |
final Class<?> mapClass; |
107 |
|
volatile int matches; |
108 |
< |
Hasher(String name, Object[] elts, Class mapClass) { |
108 |
> |
Hasher(String name, Object[] elts, Class<?> mapClass) { |
109 |
|
super(name); |
110 |
|
this.elts = elts; |
111 |
|
this.mapClass = mapClass; |
151 |
|
} |
152 |
|
|
153 |
|
public static void main(String[] args) throws Throwable { |
154 |
< |
Class mc = java.util.HashMap.class; |
154 |
> |
Class<?> mc = java.util.HashMap.class; |
155 |
|
if (args.length > 0) |
156 |
|
mc = Class.forName(args[0]); |
157 |
|
if (args.length > 1) |
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 |
|
|
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); |
193 |
|
Thread.sleep(50); |
194 |
|
} |
195 |
|
System.out.println("starting"); |
196 |
< |
time(jobs); |
196 |
> |
time(jobs); |
197 |
|
} |
198 |
|
|
199 |
|
} |