Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/util/HashMapToArray.java
41161 views
1
/*
2
* Copyright (c) 2019, Oracle and/or its affiliates. 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
package org.openjdk.bench.java.util;
24
25
import org.openjdk.jmh.annotations.Benchmark;
26
import org.openjdk.jmh.annotations.BenchmarkMode;
27
import org.openjdk.jmh.annotations.Fork;
28
import org.openjdk.jmh.annotations.Measurement;
29
import org.openjdk.jmh.annotations.Mode;
30
import org.openjdk.jmh.annotations.OutputTimeUnit;
31
import org.openjdk.jmh.annotations.Param;
32
import org.openjdk.jmh.annotations.Scope;
33
import org.openjdk.jmh.annotations.Setup;
34
import org.openjdk.jmh.annotations.State;
35
import org.openjdk.jmh.annotations.Warmup;
36
37
import java.util.Arrays;
38
import java.util.HashMap;
39
import java.util.LinkedHashMap;
40
import java.util.Map;
41
import java.util.concurrent.TimeUnit;
42
43
@BenchmarkMode(Mode.AverageTime)
44
@OutputTimeUnit(TimeUnit.NANOSECONDS)
45
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
46
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
47
@Fork(3)
48
@State(Scope.Thread)
49
public class HashMapToArray {
50
51
@Param({"0", "1", "10", "1000", "100000"})
52
public int size;
53
54
@Param({"HashMap", "LinkedHashMap"})
55
public String mapType;
56
57
private Map<Integer, Integer> map;
58
59
@Setup
60
public void setup() {
61
switch (mapType) {
62
case "HashMap":
63
map = new HashMap<>();
64
break;
65
case "LinkedHashMap":
66
map = new LinkedHashMap<>();
67
break;
68
default:
69
throw new IllegalStateException();
70
}
71
for (int i = 0; i < size; i++) {
72
map.put(i, i * i);
73
}
74
}
75
76
@Benchmark
77
public Object[] testKeySetToArray() {
78
return map.keySet().toArray();
79
}
80
81
@Benchmark
82
public Object[] testKeySetToArrayTyped() {
83
return map.keySet().toArray(new Integer[0]);
84
}
85
86
@Benchmark
87
public Object[] testValuesToArray() {
88
return map.values().toArray();
89
}
90
91
@Benchmark
92
public Object[] testValuesToArrayTyped() {
93
return map.values().toArray(new Integer[0]);
94
}
95
}
96
97