Skip to content

Commit 1bdf1be

Browse files
author
Bruce Eckel
committed
Working draft of understandingcollections/jmh
1 parent e8db171 commit 1bdf1be

9 files changed

Lines changed: 258 additions & 171 deletions

File tree

understandingcollections/CountedString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public CountedString(String str) {
1414
s = str;
1515
created.add(s);
1616
// id is the total number of instances
17-
// of this String in use by CountedString:
17+
// of this String used by CountedString:
1818
for(String s2 : created)
1919
if(s2.equals(s))
2020
id++;

understandingcollections/SuppliersCollectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void main(String[] args) {
2929
Suppliers.fill(list, new Government(), 15);
3030
System.out.println(list);
3131

32-
// Or we could just use Streams:
32+
// Or we can use Streams:
3333
set = Arrays.stream(Government.foundation).collect(
3434
Collectors.toSet());
3535
System.out.println(set);

understandingcollections/ToDoList.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public void add(String td, char pri, int sec) {
3636
super.add(new ToDoItem(td, pri, sec));
3737
}
3838
public static void main(String[] args) {
39-
ToDoList toDoList = new ToDoList();
40-
toDoList.add("Empty trash", 'C', 4);
41-
toDoList.add("Feed dog", 'A', 2);
42-
toDoList.add("Feed bird", 'B', 7);
43-
toDoList.add("Mow lawn", 'C', 3);
44-
toDoList.add("Water lawn", 'A', 1);
45-
toDoList.add("Feed cat", 'B', 1);
46-
while(!toDoList.isEmpty())
47-
System.out.println(toDoList.remove());
39+
ToDoList toDo = new ToDoList();
40+
toDo.add("Empty trash", 'C', 4);
41+
toDo.add("Feed dog", 'A', 2);
42+
toDo.add("Feed bird", 'B', 7);
43+
toDo.add("Mow lawn", 'C', 3);
44+
toDo.add("Water lawn", 'A', 1);
45+
toDo.add("Feed cat", 'B', 1);
46+
while(!toDo.isEmpty())
47+
System.out.println(toDo.remove());
4848
}
4949
}
5050
/* Output:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// understandingcollections/jmh/Deques.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Performance differences between Deques
6+
package understandingcollections.jmh;
7+
import org.openjdk.jmh.annotations.*;
8+
import org.openjdk.jmh.infra.Blackhole;
9+
import java.util.concurrent.TimeUnit;
10+
import java.util.*;
11+
12+
@State(Scope.Thread)
13+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
14+
@Warmup(iterations = 5, batchSize = 5000)
15+
@Measurement(iterations = 5, batchSize = 5000)
16+
@BenchmarkMode(Mode.SingleShotTime)
17+
@Fork(1)
18+
public class Deques {
19+
private Deque<String> deque;
20+
21+
@Param({
22+
"java.util.LinkedList",
23+
"java.util.ArrayDeque",
24+
"java.util.concurrent.ConcurrentLinkedDeque",
25+
"java.util.concurrent.LinkedBlockingDeque",
26+
})
27+
private String type;
28+
29+
@Param({
30+
"1",
31+
"10",
32+
"100",
33+
"1000",
34+
"10000",
35+
"100000"
36+
})
37+
private int size;
38+
39+
@Setup
40+
public void setup() {
41+
try {
42+
deque = (Deque<String>)
43+
Class.forName(type).newInstance();
44+
} catch(Exception e) {
45+
System.err.println(
46+
"-> Cannot create: " + type);
47+
System.exit(99);
48+
}
49+
for(int i = 0; i < size; i++)
50+
deque.add(Integer.toString(i));
51+
}
52+
@Benchmark
53+
public Deque<String> addFirst() {
54+
deque.addFirst("test");
55+
return deque;
56+
}
57+
@Benchmark
58+
public Deque<String> addLast() {
59+
deque.addLast("test");
60+
return deque;
61+
}
62+
@Benchmark
63+
public void pollFirst(Blackhole bh) {
64+
bh.consume(deque.pollFirst());
65+
}
66+
@Benchmark
67+
public void pollLast(Blackhole bh) {
68+
bh.consume(deque.pollLast());
69+
}
70+
}

understandingcollections/jmh/Lists.java

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,90 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5-
// Demonstrates performance differences in Lists
5+
// Performance differences between Lists
66
package understandingcollections.jmh;
77
import org.openjdk.jmh.annotations.*;
88
import org.openjdk.jmh.infra.Blackhole;
99
import java.util.*;
10-
import static java.util.concurrent.TimeUnit.*;
10+
import java.util.concurrent.*;
1111

1212
@State(Scope.Thread)
13-
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS)
14-
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS)
13+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
14+
@Warmup(iterations = 5, batchSize = 5000)
15+
@Measurement(iterations = 5, batchSize = 5000)
16+
@BenchmarkMode(Mode.SingleShotTime)
1517
@Fork(1)
16-
@BenchmarkMode(Mode.AverageTime)
17-
@OutputTimeUnit(MICROSECONDS)
1818
public class Lists {
19-
private List<Integer> list;
19+
private List<String> list;
2020

21-
@Param({"ArrayList", "LinkedList", "Vector"})
21+
@Param({
22+
"java.util.ArrayList",
23+
"java.util.Vector",
24+
"java.util.LinkedList",
25+
"java.util.concurrent.CopyOnWriteArrayList"
26+
})
2227
private String type;
2328

24-
private int begin;
25-
private int end;
29+
@Param({
30+
"1",
31+
"10",
32+
"100",
33+
"1000",
34+
"10000",
35+
"100000"
36+
})
37+
private int size;
2638

2739
@Setup
2840
public void setup() {
29-
switch(type) {
30-
case "ArrayList":
31-
list = new ArrayList<>();
32-
break;
33-
case "LinkedList":
34-
list = new LinkedList<>();
35-
break;
36-
case "Vector":
37-
list = new Vector<>();
38-
break;
39-
default:
40-
throw new IllegalStateException("Unknown " + type);
41-
}
42-
begin = 0;
43-
end = 256;
44-
for(int i = begin; i < end; i++) {
45-
list.add(i);
41+
try {
42+
list = (List<String>)
43+
Class.forName(type).newInstance();
44+
} catch(Exception e) {
45+
System.err.println(
46+
"-> Cannot create: " + type);
47+
System.exit(99);
4648
}
49+
for(int i = 0; i < size; i++)
50+
list.add(Integer.toString(i));
4751
}
4852
@Benchmark
49-
public void add() {
50-
for(int i = begin; i < end; i++)
51-
list.add(i);
53+
public List<String> add() {
54+
list.add(list.size() / 2, "test");
55+
return list;
5256
}
5357
@Benchmark
5458
public void get(Blackhole bh) {
55-
for(int i = begin; i < end; i++)
56-
bh.consume(list.get(i));
59+
bh.consume(list.get(list.size() / 2));
5760
}
5861
@Benchmark
59-
public void set() {
60-
for(int i = begin; i < end; i++)
61-
list.set(i, 47);
62+
public List<String> set() {
63+
list.set(list.size() / 2, "test");
64+
return list;
6265
}
6366
@Benchmark
64-
public void iteradd() {
65-
int half = list.size() / 2;
66-
ListIterator<Integer> it =
67-
list.listIterator(half);
68-
for(int i = begin; i < end; i++)
69-
it.add(47);
67+
public List<String> iteradd() {
68+
ListIterator<String> it =
69+
list.listIterator(list.size() / 2);
70+
try {
71+
it.add("test");
72+
} catch(UnsupportedOperationException e) {
73+
System.err.println(
74+
"-> Unsupported: listIterator.add() in " +
75+
list.getClass().getSimpleName());
76+
}
77+
return list;
7078
}
7179
@Benchmark
72-
public void insert() {
73-
for(int i = begin; i < end; i++)
74-
list.add(5, 47);
80+
public List<String> insert() {
81+
list.add(list.size() / 2, "test");
82+
return list;
7583
}
7684
@Benchmark
77-
public void remove() {
78-
while(list.size() > 5)
79-
list.remove(5);
85+
public List<String> remove() {
86+
int index = list.size() / 2;
87+
if(index > 0)
88+
list.remove(index);
89+
return list;
8090
}
8191
}

understandingcollections/jmh/Maps.java

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,60 @@
77
import org.openjdk.jmh.annotations.*;
88
import org.openjdk.jmh.infra.Blackhole;
99
import java.util.*;
10-
import static java.util.concurrent.TimeUnit.*;
10+
import java.util.concurrent.*;
1111

1212
@State(Scope.Thread)
13-
@Warmup(iterations = 5, time = 1, timeUnit = SECONDS)
14-
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS)
13+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
14+
@Warmup(iterations = 5, batchSize = 5000)
15+
@Measurement(iterations = 5, batchSize = 5000)
16+
@BenchmarkMode(Mode.SingleShotTime)
1517
@Fork(1)
16-
@BenchmarkMode(Mode.AverageTime)
17-
@OutputTimeUnit(MICROSECONDS)
1818
public class Maps {
19-
private Map<Integer, Integer> map;
19+
private Map<String,String> map;
2020

21-
@Param({"HashMap", "TreeMap", "LinkedHashMap",
22-
"IdentityHashMap", "WeakHashMap", "Hashtable",})
21+
@Param({
22+
"java.util.HashMap",
23+
"java.util.Hashtable",
24+
"java.util.TreeMap",
25+
"java.util.LinkedHashMap",
26+
"java.util.IdentityHashMap",
27+
"java.util.WeakHashMap",
28+
"java.util.concurrent.ConcurrentHashMap",
29+
"java.util.concurrent.ConcurrentSkipListMap",
30+
})
2331
private String type;
2432

25-
private int begin;
26-
private int end;
33+
@Param({
34+
"1",
35+
"10",
36+
"100",
37+
"1000",
38+
"10000",
39+
})
40+
private int size;
2741

2842
@Setup
2943
public void setup() {
30-
switch(type) {
31-
case "HashMap":
32-
map = new HashMap<>();
33-
break;
34-
case "TreeMap":
35-
map = new TreeMap<>();
36-
break;
37-
case "LinkedHashMap":
38-
map = new LinkedHashMap<>();
39-
break;
40-
case "IdentityHashMap":
41-
map = new IdentityHashMap<>();
42-
break;
43-
case "WeakHashMap":
44-
map = new WeakHashMap<>();
45-
break;
46-
case "Hashtable":
47-
map = new Hashtable<>();
48-
break;
49-
default:
50-
throw new IllegalStateException("Unknown " + type);
51-
}
52-
begin = 1;
53-
end = 256;
54-
for (int i = begin; i < end; i++) {
55-
map.put(i, i);
44+
try {
45+
map = (Map<String,String>)
46+
Class.forName(type).newInstance();
47+
} catch(Exception e) {
48+
System.err.println(
49+
"-> Cannot create: " + type);
50+
System.exit(99);
5651
}
52+
for(int i = 0; i < size; i++)
53+
map.put(Integer.toString(i), Integer.toString(i));
5754
}
5855
@Benchmark
5956
public void get(Blackhole bh) {
60-
for (int i = begin; i < end; i++) {
61-
bh.consume(map.get(i));
62-
}
57+
String key = Integer.toString(size / 2);
58+
bh.consume(map.get(key));
6359
}
6460
@Benchmark
65-
public void put() {
66-
for (int i = begin; i < end; i++) {
67-
map.put(i, i);
68-
}
61+
public Map<String,String> put() {
62+
map.put("test", "test");
63+
return map;
6964
}
7065
@Benchmark
7166
public void iterate(Blackhole bh) {

0 commit comments

Comments
 (0)