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