|
2 | 2 | // (c)2016 MindView LLC: see Copyright.txt |
3 | 3 | // We make no guarantees that this code is fit for any purpose. |
4 | 4 | // Visit http://OnJava8.com for more book information. |
5 | | -// Demonstrates performance differences in Lists |
| 5 | +// Performance differences between Lists |
6 | 6 | package understandingcollections.jmh; |
7 | 7 | import org.openjdk.jmh.annotations.*; |
8 | 8 | import org.openjdk.jmh.infra.Blackhole; |
9 | 9 | import java.util.*; |
10 | | -import static java.util.concurrent.TimeUnit.*; |
| 10 | +import java.util.concurrent.*; |
11 | 11 |
|
12 | 12 | @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) |
15 | 17 | @Fork(1) |
16 | | -@BenchmarkMode(Mode.AverageTime) |
17 | | -@OutputTimeUnit(MICROSECONDS) |
18 | 18 | public class Lists { |
19 | | - private List<Integer> list; |
| 19 | + private List<String> list; |
20 | 20 |
|
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 | + }) |
22 | 27 | private String type; |
23 | 28 |
|
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; |
26 | 38 |
|
27 | 39 | @Setup |
28 | 40 | 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); |
46 | 48 | } |
| 49 | + for(int i = 0; i < size; i++) |
| 50 | + list.add(Integer.toString(i)); |
47 | 51 | } |
48 | 52 | @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; |
52 | 56 | } |
53 | 57 | @Benchmark |
54 | 58 | 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)); |
57 | 60 | } |
58 | 61 | @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; |
62 | 65 | } |
63 | 66 | @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; |
70 | 78 | } |
71 | 79 | @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; |
75 | 83 | } |
76 | 84 | @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; |
80 | 90 | } |
81 | 91 | } |
0 commit comments