1 /* 2 * Copyright (c) 2015, 2015, 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 micro.benchmarks; 24 25 import java.util.Arrays; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.Level; 29 import org.openjdk.jmh.annotations.OperationsPerInvocation; 30 import org.openjdk.jmh.annotations.Scope; 31 import org.openjdk.jmh.annotations.Setup; 32 import org.openjdk.jmh.annotations.State; 33 import org.openjdk.jmh.annotations.TearDown; 34 35 import org.graalvm.compiler.microbenchmarks.graal.GraalBenchmark; 36 37 @State(Scope.Thread) 38 public class ArrayDuplicationBenchmark extends GraalBenchmark { 39 40 /** How large should the test-arrays be. */ 41 private static final int TESTSIZE = 300; 42 43 private Object[][] testObjectArray; 44 45 private Object[] dummy; 46 47 @Setup 48 public void setup() { 49 testObjectArray = new Object[TESTSIZE][]; 50 for (int i = 0; i < TESTSIZE; i++) { 51 testObjectArray[i] = new Object[20]; 52 } 53 } 54 55 @Setup(Level.Iteration) 56 public void iterationSetup() { 57 dummy = new Object[TESTSIZE * 3]; 58 } 59 60 @TearDown(Level.Iteration) 61 public void iterationTearDown() { 62 dummy = null; 63 } 64 65 @Benchmark 66 @OperationsPerInvocation(TESTSIZE) 67 public Object[] normalArraycopy() { 68 int j = 0; 69 for (int i = 0; i < TESTSIZE; i++) { 70 dummy[j++] = normalArraycopy(testObjectArray[i]); 71 } 72 return dummy; 73 } 74 75 public Object[] normalArraycopy(Object[] cache) { 76 Object[] result = new Object[cache.length]; 77 System.arraycopy(cache, 0, result, 0, result.length); 78 return result; 79 } 80 81 @Benchmark 82 @OperationsPerInvocation(TESTSIZE) 83 public Object[] arraysCopyOf() { 84 int j = 0; 85 for (int i = 0; i < TESTSIZE; i++) { 86 dummy[j++] = arraysCopyOf(testObjectArray[i]); 87 } 88 return dummy; 89 } 90 91 public Object[] arraysCopyOf(Object[] cache) { 92 return Arrays.copyOf(cache, cache.length); 93 } 94 95 @Benchmark 96 @OperationsPerInvocation(TESTSIZE) 97 public Object[] cloneObjectArray() { 98 int j = 0; 99 for (int i = 0; i < TESTSIZE; i++) { 100 dummy[j++] = arraysClone(testObjectArray[i]); 101 } 102 return dummy; 103 } 104 105 @SuppressWarnings("cast") 106 public Object[] arraysClone(Object[] cache) { 107 return (Object[]) cache.clone(); 108 } 109 110 }