forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaps.java
More file actions
76 lines (72 loc) · 1.9 KB
/
Copy pathMaps.java
File metadata and controls
76 lines (72 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// understandingcollections/jmh/Maps.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Performance differences between Maps
package understandingcollections.jmh;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.*;
import static java.util.concurrent.TimeUnit.*;
@State(Scope.Thread)
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS)
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(MICROSECONDS)
public class Maps {
private Map<Integer, Integer> map;
@Param({"HashMap", "TreeMap", "LinkedHashMap",
"IdentityHashMap", "WeakHashMap", "Hashtable",})
private String type;
private int begin;
private int end;
@Setup
public void setup() {
switch(type) {
case "HashMap":
map = new HashMap<>();
break;
case "TreeMap":
map = new TreeMap<>();
break;
case "LinkedHashMap":
map = new LinkedHashMap<>();
break;
case "IdentityHashMap":
map = new IdentityHashMap<>();
break;
case "WeakHashMap":
map = new WeakHashMap<>();
break;
case "Hashtable":
map = new Hashtable<>();
break;
default:
throw new IllegalStateException("Unknown " + type);
}
begin = 1;
end = 256;
for (int i = begin; i < end; i++) {
map.put(i, i);
}
}
@Benchmark
public void get(Blackhole bh) {
for (int i = begin; i < end; i++) {
bh.consume(map.get(i));
}
}
@Benchmark
public void put() {
for (int i = begin; i < end; i++) {
map.put(i, i);
}
}
@Benchmark
public void iterate(Blackhole bh) {
Iterator it = map.entrySet().iterator();
while(it.hasNext())
bh.consume(it.next());
}
}