Working draft of understandingcollections/jmh

This commit is contained in:
Bruce Eckel 2016-10-22 13:35:19 -07:00
parent e8db17143e
commit 1bdf1beb30
9 changed files with 258 additions and 171 deletions

View File

@ -14,7 +14,7 @@ public class CountedString {
s = str; s = str;
created.add(s); created.add(s);
// id is the total number of instances // id is the total number of instances
// of this String in use by CountedString: // of this String used by CountedString:
for(String s2 : created) for(String s2 : created)
if(s2.equals(s)) if(s2.equals(s))
id++; id++;

View File

@ -29,7 +29,7 @@ public class SuppliersCollectionTest {
Suppliers.fill(list, new Government(), 15); Suppliers.fill(list, new Government(), 15);
System.out.println(list); System.out.println(list);
// Or we could just use Streams: // Or we can use Streams:
set = Arrays.stream(Government.foundation).collect( set = Arrays.stream(Government.foundation).collect(
Collectors.toSet()); Collectors.toSet());
System.out.println(set); System.out.println(set);

View File

@ -36,15 +36,15 @@ class ToDoList extends PriorityQueue<ToDoList.ToDoItem> {
super.add(new ToDoItem(td, pri, sec)); super.add(new ToDoItem(td, pri, sec));
} }
public static void main(String[] args) { public static void main(String[] args) {
ToDoList toDoList = new ToDoList(); ToDoList toDo = new ToDoList();
toDoList.add("Empty trash", 'C', 4); toDo.add("Empty trash", 'C', 4);
toDoList.add("Feed dog", 'A', 2); toDo.add("Feed dog", 'A', 2);
toDoList.add("Feed bird", 'B', 7); toDo.add("Feed bird", 'B', 7);
toDoList.add("Mow lawn", 'C', 3); toDo.add("Mow lawn", 'C', 3);
toDoList.add("Water lawn", 'A', 1); toDo.add("Water lawn", 'A', 1);
toDoList.add("Feed cat", 'B', 1); toDo.add("Feed cat", 'B', 1);
while(!toDoList.isEmpty()) while(!toDo.isEmpty())
System.out.println(toDoList.remove()); System.out.println(toDo.remove());
} }
} }
/* Output: /* Output:

View File

@ -0,0 +1,70 @@
// understandingcollections/jmh/Deques.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Performance differences between Deques
package understandingcollections.jmh;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.TimeUnit;
import java.util.*;
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5, batchSize = 5000)
@Measurement(iterations = 5, batchSize = 5000)
@BenchmarkMode(Mode.SingleShotTime)
@Fork(1)
public class Deques {
private Deque<String> deque;
@Param({
"java.util.LinkedList",
"java.util.ArrayDeque",
"java.util.concurrent.ConcurrentLinkedDeque",
"java.util.concurrent.LinkedBlockingDeque",
})
private String type;
@Param({
"1",
"10",
"100",
"1000",
"10000",
"100000"
})
private int size;
@Setup
public void setup() {
try {
deque = (Deque<String>)
Class.forName(type).newInstance();
} catch(Exception e) {
System.err.println(
"-> Cannot create: " + type);
System.exit(99);
}
for(int i = 0; i < size; i++)
deque.add(Integer.toString(i));
}
@Benchmark
public Deque<String> addFirst() {
deque.addFirst("test");
return deque;
}
@Benchmark
public Deque<String> addLast() {
deque.addLast("test");
return deque;
}
@Benchmark
public void pollFirst(Blackhole bh) {
bh.consume(deque.pollFirst());
}
@Benchmark
public void pollLast(Blackhole bh) {
bh.consume(deque.pollLast());
}
}

View File

@ -2,80 +2,90 @@
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Demonstrates performance differences in Lists // Performance differences between Lists
package understandingcollections.jmh; package understandingcollections.jmh;
import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.infra.Blackhole;
import java.util.*; import java.util.*;
import static java.util.concurrent.TimeUnit.*; import java.util.concurrent.*;
@State(Scope.Thread) @State(Scope.Thread)
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS) @OutputTimeUnit(TimeUnit.MICROSECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS) @Warmup(iterations = 5, batchSize = 5000)
@Measurement(iterations = 5, batchSize = 5000)
@BenchmarkMode(Mode.SingleShotTime)
@Fork(1) @Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(MICROSECONDS)
public class Lists { public class Lists {
private List<Integer> list; private List<String> list;
@Param({"ArrayList", "LinkedList", "Vector"}) @Param({
"java.util.ArrayList",
"java.util.Vector",
"java.util.LinkedList",
"java.util.concurrent.CopyOnWriteArrayList"
})
private String type; private String type;
private int begin; @Param({
private int end; "1",
"10",
"100",
"1000",
"10000",
"100000"
})
private int size;
@Setup @Setup
public void setup() { public void setup() {
switch(type) { try {
case "ArrayList": list = (List<String>)
list = new ArrayList<>(); Class.forName(type).newInstance();
break; } catch(Exception e) {
case "LinkedList": System.err.println(
list = new LinkedList<>(); "-> Cannot create: " + type);
break; System.exit(99);
case "Vector":
list = new Vector<>();
break;
default:
throw new IllegalStateException("Unknown " + type);
}
begin = 0;
end = 256;
for(int i = begin; i < end; i++) {
list.add(i);
} }
for(int i = 0; i < size; i++)
list.add(Integer.toString(i));
} }
@Benchmark @Benchmark
public void add() { public List<String> add() {
for(int i = begin; i < end; i++) list.add(list.size() / 2, "test");
list.add(i); return list;
} }
@Benchmark @Benchmark
public void get(Blackhole bh) { public void get(Blackhole bh) {
for(int i = begin; i < end; i++) bh.consume(list.get(list.size() / 2));
bh.consume(list.get(i));
} }
@Benchmark @Benchmark
public void set() { public List<String> set() {
for(int i = begin; i < end; i++) list.set(list.size() / 2, "test");
list.set(i, 47); return list;
} }
@Benchmark @Benchmark
public void iteradd() { public List<String> iteradd() {
int half = list.size() / 2; ListIterator<String> it =
ListIterator<Integer> it = list.listIterator(list.size() / 2);
list.listIterator(half); try {
for(int i = begin; i < end; i++) it.add("test");
it.add(47); } catch(UnsupportedOperationException e) {
System.err.println(
"-> Unsupported: listIterator.add() in " +
list.getClass().getSimpleName());
}
return list;
} }
@Benchmark @Benchmark
public void insert() { public List<String> insert() {
for(int i = begin; i < end; i++) list.add(list.size() / 2, "test");
list.add(5, 47); return list;
} }
@Benchmark @Benchmark
public void remove() { public List<String> remove() {
while(list.size() > 5) int index = list.size() / 2;
list.remove(5); if(index > 0)
list.remove(index);
return list;
} }
} }

View File

@ -7,65 +7,60 @@ package understandingcollections.jmh;
import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.infra.Blackhole;
import java.util.*; import java.util.*;
import static java.util.concurrent.TimeUnit.*; import java.util.concurrent.*;
@State(Scope.Thread) @State(Scope.Thread)
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS) @OutputTimeUnit(TimeUnit.MICROSECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS) @Warmup(iterations = 5, batchSize = 5000)
@Measurement(iterations = 5, batchSize = 5000)
@BenchmarkMode(Mode.SingleShotTime)
@Fork(1) @Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(MICROSECONDS)
public class Maps { public class Maps {
private Map<Integer, Integer> map; private Map<String,String> map;
@Param({"HashMap", "TreeMap", "LinkedHashMap", @Param({
"IdentityHashMap", "WeakHashMap", "Hashtable",}) "java.util.HashMap",
"java.util.Hashtable",
"java.util.TreeMap",
"java.util.LinkedHashMap",
"java.util.IdentityHashMap",
"java.util.WeakHashMap",
"java.util.concurrent.ConcurrentHashMap",
"java.util.concurrent.ConcurrentSkipListMap",
})
private String type; private String type;
private int begin; @Param({
private int end; "1",
"10",
"100",
"1000",
"10000",
})
private int size;
@Setup @Setup
public void setup() { public void setup() {
switch(type) { try {
case "HashMap": map = (Map<String,String>)
map = new HashMap<>(); Class.forName(type).newInstance();
break; } catch(Exception e) {
case "TreeMap": System.err.println(
map = new TreeMap<>(); "-> Cannot create: " + type);
break; System.exit(99);
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);
} }
for(int i = 0; i < size; i++)
map.put(Integer.toString(i), Integer.toString(i));
} }
@Benchmark @Benchmark
public void get(Blackhole bh) { public void get(Blackhole bh) {
for (int i = begin; i < end; i++) { String key = Integer.toString(size / 2);
bh.consume(map.get(i)); bh.consume(map.get(key));
}
} }
@Benchmark @Benchmark
public void put() { public Map<String,String> put() {
for (int i = begin; i < end; i++) { map.put("test", "test");
map.put(i, i); return map;
}
} }
@Benchmark @Benchmark
public void iterate(Blackhole bh) { public void iterate(Blackhole bh) {

View File

@ -2,61 +2,68 @@
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
// Demonstrates performance differences in Queues // Performance differences between Queues
package understandingcollections.jmh; package understandingcollections.jmh;
import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.infra.Blackhole;
import java.util.*; import java.util.*;
import static java.util.concurrent.TimeUnit.*; import java.util.concurrent.*;
@State(Scope.Thread) @State(Scope.Thread)
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS) @OutputTimeUnit(TimeUnit.MICROSECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS) @Warmup(iterations = 5, batchSize = 5000)
@Measurement(iterations = 5, batchSize = 5000)
@BenchmarkMode(Mode.SingleShotTime)
@Fork(1) @Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(MICROSECONDS)
public class Queues { public class Queues {
private LinkedList<Integer> queue; private Queue<String> queue;
@Param({"LinkedList"}) @Param({
"java.util.LinkedList",
"java.util.concurrent.ConcurrentLinkedQueue",
"java.util.concurrent.LinkedBlockingQueue",
"java.util.concurrent.LinkedTransferQueue",
"java.util.concurrent.PriorityBlockingQueue",
"java.util.PriorityQueue",
// Requires a size for construction:
//"java.util.concurrent.ArrayBlockingQueue",
// Won't accept more than one element:
//"java.util.concurrent.SynchronousQueue",
// Requires "Delayed" elements:
//"java.util.concurrent.DelayQueue",
})
private String type; private String type;
private int begin; @Param({
private int end; "1",
"10",
"100",
"1000",
"10000",
"100000"
})
private int size;
@Setup @Setup
public void setup() { public void setup() {
switch(type) { try {
case "LinkedList": queue = (Queue<String>)
queue = new LinkedList<>(); Class.forName(type).newInstance();
break; } catch(Exception e) {
default: System.err.println(
throw new IllegalStateException("Unknown " + type); "-> Cannot create: " + type);
} System.exit(99);
begin = 1;
end = 256;
for(int i = begin; i < end; i++) {
queue.add(i);
} }
for(int i = 0; i < size; i++)
queue.add(Integer.toString(i));
} }
@Benchmark @Benchmark
public void queue_addFirst() { public Queue<String> add() {
for(int i = begin; i < end; i++) queue.add("test");
queue.addFirst(47); return queue;
} }
@Benchmark @Benchmark
public void queue_addLast() { public void poll(Blackhole bh) {
for(int i = begin; i < end; i++) bh.consume(queue.poll());
queue.addLast(47);
}
@Benchmark
public void queue_removeFirst(Blackhole bh) {
while(queue.size() > 0)
bh.consume(queue.removeFirst());
}
@Benchmark
public void queue_removeLast(Blackhole bh) {
while(queue.size() > 0)
bh.consume(queue.removeLast());
} }
} }

View File

@ -7,56 +7,61 @@ package understandingcollections.jmh;
import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.infra.Blackhole;
import java.util.*; import java.util.*;
import static java.util.concurrent.TimeUnit.*; import java.util.concurrent.*;
@State(Scope.Thread) @State(Scope.Thread)
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS) @OutputTimeUnit(TimeUnit.MICROSECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS) @Warmup(iterations = 5, batchSize = 5000)
@Measurement(iterations = 5, batchSize = 5000)
@BenchmarkMode(Mode.SingleShotTime)
@Fork(1) @Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(MICROSECONDS)
public class Sets { public class Sets {
private Set<Integer> set; private Set<String> set;
@Param({"HashSet", "TreeSet", "LinkedHashSet"}) @Param({
"java.util.HashSet",
"java.util.TreeSet",
"java.util.LinkedHashSet",
"java.util.concurrent.ConcurrentSkipListSet",
"java.util.concurrent.CopyOnWriteArraySet",
})
private String type; private String type;
private int begin; @Param({
private int end; "1",
"10",
"100",
"1000",
"10000",
})
private int size;
@Setup @Setup
public void setup() { public void setup() {
switch(type) { try {
case "HashSet": set = (Set<String>)
set = new HashSet<>(); Class.forName(type).newInstance();
break; } catch(Exception e) {
case "TreeSet": System.err.println(
set = new TreeSet<>(); "-> Cannot create: " + type);
break; System.exit(99);
case "LinkedHashSet":
set = new LinkedHashSet<>();
break;
default:
throw new IllegalStateException("Unknown " + type);
} }
begin = 1; for(int i = 0; i < size; i++)
end = 256; set.add(Integer.toString(i));
for (int i = begin; i < end; i++)
set.add(i);
} }
@Benchmark @Benchmark
public void add() { public Set<String> add() {
for(int i = begin; i < end; i++) set.add("test");
set.add(i); return set;
} }
@Benchmark @Benchmark
public void contains(Blackhole bh) { public void contains(Blackhole bh) {
for(int i = begin; i < end; i++) String key = Integer.toString(size/2);
bh.consume(set.contains(i)); bh.consume(set.contains(key));
} }
@Benchmark @Benchmark
public void iterate(Blackhole bh) { public void iterate(Blackhole bh) {
Iterator<Integer> it = set.iterator(); Iterator<String> it = set.iterator();
while(it.hasNext()) while(it.hasNext())
bh.consume(it.next()); bh.consume(it.next());
} }

View File

@ -34,8 +34,8 @@ public class JMH3 {
la = new long[size]; la = new long[size];
} }
public static long f(long x) { public static long f(long x) {
long divisor = x % 25 + 1; long quadratic = 42 * x * x + 19 * x + 47;
return Long.divideUnsigned(x, divisor); return Long.divideUnsigned(quadratic, x + 1);
} }
@Benchmark @Benchmark
public void setAll() { public void setAll() {