Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/lang/NewInstance.java
41161 views
1
/*
2
* Copyright (c) 2014, 2020, 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.lang;
24
25
import org.openjdk.jmh.annotations.Benchmark;
26
import org.openjdk.jmh.annotations.BenchmarkMode;
27
import org.openjdk.jmh.annotations.Mode;
28
import org.openjdk.jmh.annotations.OutputTimeUnit;
29
import org.openjdk.jmh.annotations.Scope;
30
import org.openjdk.jmh.annotations.Setup;
31
import org.openjdk.jmh.annotations.State;
32
import org.openjdk.jmh.infra.Blackhole;
33
34
import java.util.concurrent.TimeUnit;
35
36
/**
37
* Benchmark measuring java.lang.Class.newInstance speed.
38
*/
39
@BenchmarkMode(Mode.AverageTime)
40
@OutputTimeUnit(TimeUnit.NANOSECONDS)
41
@State(Scope.Thread)
42
public class NewInstance {
43
44
public Class<?>[] samePublicClasses;
45
public Class<?>[] differentPublicClasses;
46
public Class<?>[] differentPublicClassesConstant;
47
public Class<?>[] sameProtectedClasses;
48
public Class<?>[] differentProtectedClasses;
49
50
@Setup
51
public void setup() {
52
samePublicClasses = new Class<?>[]{Apub.class, Apub.class, Apub.class};
53
differentPublicClasses = new Class<?>[]{Apub.class, Bpub.class, Cpub.class};
54
differentPublicClassesConstant = new Class<?>[]{Apub.class, Bpub.class, Cpub.class};
55
sameProtectedClasses = new Class<?>[]{Apro.class, Apro.class, Apro.class};
56
differentProtectedClasses = new Class<?>[]{Apro.class, Bpro.class, Cpro.class};
57
}
58
59
/**
60
* Performs Class.newInstance on the same class over and over again. That it is the same class is not provable at
61
* compile time. The class is protected.
62
*/
63
@SuppressWarnings("deprecation")
64
@Benchmark
65
public void threeSameProtected(Blackhole bh) throws IllegalAccessException, InstantiationException {
66
for (Class<?> cl : sameProtectedClasses) {
67
bh.consume(cl.newInstance());
68
}
69
}
70
71
/**
72
* Performs Class.newInstance on three different classes, just allocating one instance of one class at a time. The
73
* classes are all protected.
74
*/
75
@SuppressWarnings("deprecation")
76
@Benchmark
77
public void threeDifferentProtected(Blackhole bh) throws IllegalAccessException, InstantiationException {
78
for (Class<?> cl : differentProtectedClasses) {
79
bh.consume(cl.newInstance());
80
}
81
}
82
83
/**
84
* Performs Class.newInstance on the same class over and over again. That it is the same class is not provable at
85
* compile time. The class is public.
86
*/
87
@SuppressWarnings("deprecation")
88
@Benchmark
89
public void threeSamePublic(Blackhole bh) throws IllegalAccessException, InstantiationException {
90
for (Class<?> cl : samePublicClasses) {
91
bh.consume(cl.newInstance());
92
}
93
}
94
95
/**
96
* Performs Class.newInstance on three different classes, just allocating one instance of one class at a time. The
97
* classes are all public.
98
*/
99
@SuppressWarnings("deprecation")
100
@Benchmark
101
public void threeDifferentPublic(Blackhole bh) throws IllegalAccessException, InstantiationException {
102
for (Class<?> cl : differentPublicClasses) {
103
bh.consume(cl.newInstance());
104
}
105
}
106
107
/**
108
* Performs Class.newInstance on three different classes, just allocating one instance of one class at a time. The
109
* classes are all public.
110
*/
111
@SuppressWarnings("deprecation")
112
@Benchmark
113
public void threeDifferentPublicConstant(Blackhole bh) throws IllegalAccessException, InstantiationException {
114
bh.consume(Apub.class.newInstance());
115
bh.consume(Bpub.class.newInstance());
116
bh.consume(Cpub.class.newInstance());
117
}
118
119
@SuppressWarnings("deprecation")
120
@Benchmark
121
public void threeDifferentPublicFinal(Blackhole bh) throws IllegalAccessException, InstantiationException {
122
for (Class<?> cl : differentPublicClassesConstant) {
123
bh.consume(cl.newInstance());
124
}
125
}
126
127
/* Protected test classes */
128
static class Apro {}
129
static class Bpro {}
130
static class Cpro {}
131
132
/* Public test classes */
133
public static class Apub {}
134
public static class Bpub {}
135
public static class Cpub {}
136
137
}
138
139