|
28 | 28 | import org.openjdk.jmh.annotations.Measurement;
|
29 | 29 | import org.openjdk.jmh.annotations.Mode;
|
30 | 30 | import org.openjdk.jmh.annotations.OutputTimeUnit;
|
| 31 | +import org.openjdk.jmh.annotations.Param; |
31 | 32 | import org.openjdk.jmh.annotations.Scope;
|
32 | 33 | import org.openjdk.jmh.annotations.Setup;
|
33 | 34 | import org.openjdk.jmh.annotations.State;
|
|
48 | 49 | public class Maps {
|
49 | 50 | private SimpleRandom rng;
|
50 | 51 | private Map<Integer, Integer> map;
|
| 52 | + private Map<Integer, Integer> staticMap; |
51 | 53 | private Integer[] key;
|
52 | 54 |
|
53 | 55 | private int removesPerMaxRandom;
|
54 | 56 | private int insertsPerMaxRandom;
|
55 | 57 | private int total;
|
56 | 58 | private int position;
|
57 | 59 |
|
| 60 | + @Param("10000") |
| 61 | + private int nkeys; |
| 62 | + |
58 | 63 | @Setup
|
59 | 64 | public void initTest() {
|
60 |
| - int nkeys = 10000; |
61 | 65 | int pRemove = 10;
|
62 | 66 | int pInsert = 90;
|
63 | 67 | removesPerMaxRandom = (int) ((pRemove / 100.0 * 0x7FFFFFFFL));
|
64 | 68 | insertsPerMaxRandom = (int) ((pInsert / 100.0 * 0x7FFFFFFFL));
|
65 | 69 |
|
66 | 70 | rng = new SimpleRandom();
|
67 | 71 | map = new ConcurrentHashMap<>();
|
| 72 | + staticMap = new ConcurrentHashMap<>(); |
68 | 73 | total = 0;
|
69 | 74 | key = new Integer[nkeys];
|
70 | 75 | for (int i = 0; i < key.length; ++i) {
|
71 | 76 | key[i] = rng.next();
|
| 77 | + staticMap.put(rng.next(), rng.next()); |
72 | 78 | }
|
73 | 79 | position = key.length / 2;
|
74 | 80 | }
|
@@ -106,6 +112,21 @@ public void testConcurrentHashMap() {
|
106 | 112 | position = pos;
|
107 | 113 | }
|
108 | 114 |
|
| 115 | + @Benchmark |
| 116 | + public ConcurrentHashMap<Integer, Integer> testConcurrentHashMapCopyConstructor() { |
| 117 | + return new ConcurrentHashMap<>(staticMap); |
| 118 | + } |
| 119 | + |
| 120 | + @Benchmark |
| 121 | + public ConcurrentHashMap<Integer, Integer> testConcurrentHashMapPutAll() { |
| 122 | + ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(nkeys); |
| 123 | + for (int i = 0; i < nkeys; ++i) { |
| 124 | + map.put(rng.next(), rng.next()); |
| 125 | + } |
| 126 | + map.putAll(staticMap); |
| 127 | + return map; |
| 128 | + } |
| 129 | + |
109 | 130 | private static class SimpleRandom {
|
110 | 131 | private final static long multiplier = 0x5DEECE66DL;
|
111 | 132 | private final static long addend = 0xBL;
|
|
0 commit comments