Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/util/HashMapBench.java
41161 views
1
/*
2
* Copyright (c) 2018, Red Hat, Inc. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package org.openjdk.bench.java.util;
25
26
import org.openjdk.jmh.annotations.Benchmark;
27
import org.openjdk.jmh.annotations.BenchmarkMode;
28
import org.openjdk.jmh.annotations.Mode;
29
import org.openjdk.jmh.annotations.OutputTimeUnit;
30
import org.openjdk.jmh.annotations.Param;
31
import org.openjdk.jmh.annotations.Scope;
32
import org.openjdk.jmh.annotations.Setup;
33
import org.openjdk.jmh.annotations.State;
34
35
import java.util.HashMap;
36
import java.util.LinkedHashMap;
37
import java.util.Map;
38
import java.util.concurrent.ThreadLocalRandom;
39
import java.util.concurrent.TimeUnit;
40
import java.util.function.Supplier;
41
import java.util.stream.IntStream;
42
43
import static java.util.stream.Collectors.toMap;
44
45
@BenchmarkMode(Mode.AverageTime)
46
@OutputTimeUnit(TimeUnit.MILLISECONDS)
47
@State(Scope.Thread)
48
public class HashMapBench {
49
private Supplier<Map<Integer, Integer>> mapSupplier;
50
private Map<Integer, Integer> bigMapToAdd;
51
52
@Param("1000000")
53
private int size;
54
55
@Param
56
private MapType mapType;
57
58
public enum MapType {
59
HASH_MAP,
60
LINKED_HASH_MAP,
61
}
62
63
@Setup
64
public void setup() {
65
switch (mapType) {
66
case HASH_MAP:
67
mapSupplier = () -> new HashMap<>();
68
break;
69
case LINKED_HASH_MAP:
70
mapSupplier = () -> new LinkedHashMap<>();
71
break;
72
default:
73
throw new AssertionError();
74
}
75
76
ThreadLocalRandom rnd = ThreadLocalRandom.current();
77
this.bigMapToAdd = IntStream.range(0, size).boxed()
78
.collect(toMap(i -> 7 + i * 128, i -> rnd.nextInt()));
79
}
80
81
@Benchmark
82
public int putAllWithBigMapToNonEmptyMap() {
83
Map<Integer, Integer> map = mapSupplier.get();
84
map.put(-1, -1);
85
map.putAll(bigMapToAdd);
86
return map.size();
87
}
88
89
@Benchmark
90
public int putAllWithBigMapToEmptyMap() {
91
Map<Integer, Integer> map = mapSupplier.get();
92
map.putAll(bigMapToAdd);
93
return map.size();
94
}
95
96
@Benchmark
97
public int put() {
98
Map<Integer, Integer> map = mapSupplier.get();
99
for (int k : bigMapToAdd.keySet()) {
100
map.put(k, bigMapToAdd.get(k));
101
}
102
return map.size();
103
}
104
}
105
106