Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/vm/lang/InstanceOf.java
41161 views
1
/*
2
* Copyright (c) 2014, 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.vm.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.OperationsPerInvocation;
29
import org.openjdk.jmh.annotations.OutputTimeUnit;
30
import org.openjdk.jmh.annotations.Scope;
31
import org.openjdk.jmh.annotations.Setup;
32
import org.openjdk.jmh.annotations.State;
33
34
import java.io.Serializable;
35
import java.util.Date;
36
import java.util.concurrent.TimeUnit;
37
38
/**
39
* Tests various usages of instanceof.
40
*/
41
@BenchmarkMode(Mode.AverageTime)
42
@OutputTimeUnit(TimeUnit.NANOSECONDS)
43
@State(Scope.Thread)
44
public class InstanceOf {
45
46
private static final int NOOFOBJECTS = 100;
47
private static final int NULLRATIO = 3;
48
49
public Date[] dateArray;
50
public Object[] objectArray;
51
52
@Setup
53
public void setup() {
54
dateArray = new Date[NOOFOBJECTS * NULLRATIO];
55
for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i += NULLRATIO) {
56
dateArray[i] = new Date();
57
}
58
objectArray = dateArray;
59
}
60
61
/**
62
* Performs "instanceof Cloneable" on objects that definitely are of that interface. It is not clear however whether
63
* the objects are null or not, therefore a simple nullcheck is all that should be left of here.
64
*/
65
@Benchmark
66
@OperationsPerInvocation((NOOFOBJECTS * NULLRATIO))
67
public int instanceOfInterfacePartialRemove() {
68
int dummy = 0;
69
Date[] localArray = dateArray;
70
for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i++) {
71
if (localArray[i] instanceof Cloneable) {
72
dummy++;
73
}
74
}
75
return dummy;
76
}
77
78
/**
79
* Performs three serial instanceof statements on the same object for three different interfaces. The objects are
80
* 50% null, and all non-null are instanceof the last interface.
81
*/
82
@Benchmark
83
@OperationsPerInvocation((NOOFOBJECTS * NULLRATIO))
84
public int instanceOfInterfaceSerial() {
85
int dummy = 0;
86
Object[] localArray = objectArray;
87
for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i++) {
88
if (localArray[i] instanceof Runnable) {
89
dummy += 1000;
90
} else if (localArray[i] instanceof CharSequence) {
91
dummy += 2000;
92
} else if (localArray[i] instanceof Serializable) {
93
dummy++;
94
}
95
}
96
return dummy;
97
}
98
}
99
100