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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.bench.vm.lang; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.BenchmarkMode; 29 import org.openjdk.jmh.annotations.Mode; 30 import org.openjdk.jmh.annotations.OperationsPerInvocation; 31 import org.openjdk.jmh.annotations.OutputTimeUnit; 32 import org.openjdk.jmh.annotations.Scope; 33 import org.openjdk.jmh.annotations.Setup; 34 import org.openjdk.jmh.annotations.State; 35 36 import java.io.Serializable; 37 import java.util.Date; 38 import java.util.concurrent.TimeUnit; 39 40 /** 41 * Tests various usages of instanceof. 42 */ 43 @BenchmarkMode(Mode.AverageTime) 44 @OutputTimeUnit(TimeUnit.NANOSECONDS) 45 @State(Scope.Thread) 46 public class InstanceOf { 47 48 private static final int NOOFOBJECTS = 100; 49 private static final int NULLRATIO = 3; 50 51 public Date[] dateArray; 52 public Object[] objectArray; 53 54 @Setup 55 public void setup() { 56 dateArray = new Date[NOOFOBJECTS * NULLRATIO]; 57 for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i += NULLRATIO) { 58 dateArray[i] = new Date(); 59 } 60 objectArray = dateArray; 61 } 62 63 /** 64 * Performs "instanceof Cloneable" on objects that definitely are of that interface. It is not clear however whether 65 * the objects are null or not, therefore a simple nullcheck is all that should be left of here. 66 */ 67 @Benchmark 68 @OperationsPerInvocation((NOOFOBJECTS * NULLRATIO)) 69 public int instanceOfInterfacePartialRemove() { 70 int dummy = 0; 71 Date[] localArray = dateArray; 72 for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i++) { 73 if (localArray[i] instanceof Cloneable) { 74 dummy++; 75 } 76 } 77 return dummy; 78 } 79 80 /** 81 * Performs three serial instanceof statements on the same object for three different interfaces. The objects are 82 * 50% null, and all non-null are instanceof the last interface. 83 */ 84 @Benchmark 85 @OperationsPerInvocation((NOOFOBJECTS * NULLRATIO)) 86 public int instanceOfInterfaceSerial() { 87 int dummy = 0; 88 Object[] localArray = objectArray; 89 for (int i = 0; i < NOOFOBJECTS * NULLRATIO; i++) { 90 if (localArray[i] instanceof Runnable) { 91 dummy += 1000; 92 } else if (localArray[i] instanceof CharSequence) { 93 dummy += 2000; 94 } else if (localArray[i] instanceof Serializable) { 95 dummy++; 96 } 97 } 98 return dummy; 99 } 100 }