Path: blob/master/test/micro/org/openjdk/bench/vm/lang/InstanceOf.java
41161 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.vm.lang;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OperationsPerInvocation;28import org.openjdk.jmh.annotations.OutputTimeUnit;29import org.openjdk.jmh.annotations.Scope;30import org.openjdk.jmh.annotations.Setup;31import org.openjdk.jmh.annotations.State;3233import java.io.Serializable;34import java.util.Date;35import java.util.concurrent.TimeUnit;3637/**38* Tests various usages of instanceof.39*/40@BenchmarkMode(Mode.AverageTime)41@OutputTimeUnit(TimeUnit.NANOSECONDS)42@State(Scope.Thread)43public class InstanceOf {4445private static final int NOOFOBJECTS = 100;46private static final int NULLRATIO = 3;4748public Date[] dateArray;49public Object[] objectArray;5051@Setup52public void setup() {53dateArray = new Date[NOOFOBJECTS * NULLRATIO];54for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i += NULLRATIO) {55dateArray[i] = new Date();56}57objectArray = dateArray;58}5960/**61* Performs "instanceof Cloneable" on objects that definitely are of that interface. It is not clear however whether62* the objects are null or not, therefore a simple nullcheck is all that should be left of here.63*/64@Benchmark65@OperationsPerInvocation((NOOFOBJECTS * NULLRATIO))66public int instanceOfInterfacePartialRemove() {67int dummy = 0;68Date[] localArray = dateArray;69for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i++) {70if (localArray[i] instanceof Cloneable) {71dummy++;72}73}74return dummy;75}7677/**78* Performs three serial instanceof statements on the same object for three different interfaces. The objects are79* 50% null, and all non-null are instanceof the last interface.80*/81@Benchmark82@OperationsPerInvocation((NOOFOBJECTS * NULLRATIO))83public int instanceOfInterfaceSerial() {84int dummy = 0;85Object[] localArray = objectArray;86for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i++) {87if (localArray[i] instanceof Runnable) {88dummy += 1000;89} else if (localArray[i] instanceof CharSequence) {90dummy += 2000;91} else if (localArray[i] instanceof Serializable) {92dummy++;93}94}95return dummy;96}97}9899100