Initial Reasonable JMH example working
This commit is contained in:
parent
b72a223d07
commit
71a6aa7e71
81
understandingcollections/jmhtests/MapPerformance.java
Normal file
81
understandingcollections/jmhtests/MapPerformance.java
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
// understandingcollections/jmhtests/MapPerformance.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.jmhtests;
|
||||||
|
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(NANOSECONDS)
|
||||||
|
public class MapPerformance {
|
||||||
|
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(Blackhole bh) {
|
||||||
|
for (int i = begin; i < end; i++) {
|
||||||
|
bh.consume(map.put(i, i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void iterate(Blackhole bh) {
|
||||||
|
Iterator it = map.entrySet().iterator();
|
||||||
|
while(it.hasNext())
|
||||||
|
bh.consume(it.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,66 +0,0 @@
|
|||||||
// understandingcollections/jmhtests/MapPerformanceJMH.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.
|
|
||||||
// Demonstrates performance differences in Maps
|
|
||||||
package understandingcollections.jmhtests;
|
|
||||||
import org.openjdk.jmh.annotations.*;
|
|
||||||
import org.openjdk.jmh.infra.Blackhole;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
@State(Scope.Thread)
|
|
||||||
public class MapPerformanceJMH {
|
|
||||||
private Map<Integer, Integer> map;
|
|
||||||
|
|
||||||
@Param({"hashmap", "treemap"})
|
|
||||||
private String type;
|
|
||||||
|
|
||||||
private int begin;
|
|
||||||
private int end;
|
|
||||||
|
|
||||||
@Setup
|
|
||||||
public void setup() {
|
|
||||||
if (type.equals("hashmap")) {
|
|
||||||
map = new HashMap<Integer, Integer>();
|
|
||||||
} else if (type.equals("treemap")) {
|
|
||||||
map = new TreeMap<Integer, Integer>();
|
|
||||||
} else {
|
|
||||||
throw new IllegalStateException("Unknown type: " + 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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// These are incomplete placeholders from the
|
|
||||||
// original MapPerformance.jaava:
|
|
||||||
/* @Benchmark
|
|
||||||
int put() {
|
|
||||||
for(int i = 0; i < loops; i++) {
|
|
||||||
map.clear();
|
|
||||||
for(int j = 0; j < size; j++)
|
|
||||||
map.put(j, j);
|
|
||||||
}
|
|
||||||
return loops * size;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/* @Benchmark
|
|
||||||
int iterate() {
|
|
||||||
for(int i = 0; i < loops; i ++) {
|
|
||||||
Iterator it = map.entrySet().iterator();
|
|
||||||
while(it.hasNext())
|
|
||||||
it.next();
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user