Path: blob/master/test/micro/org/openjdk/bench/java/lang/ArrayCopy.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.java.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.util.concurrent.TimeUnit;3435/**36* Benchmark measuring System.arraycopy in different ways.37*/38@BenchmarkMode(Mode.AverageTime)39@OutputTimeUnit(TimeUnit.NANOSECONDS)40@State(Scope.Thread)41public class ArrayCopy {4243private static final byte[] TEST_BYTES = "HTTP/1.0".getBytes();44private static final char[] TEST_CHARS = new char[46];45private static final Object[] TEST_OBJECTS = new Object[200]; // Uses a minimum of 160 internal positions for internal copying4647// a length which the compiler cannot prove is a constant48public static int nonConstCharLength = TEST_CHARS.length;49public static int nonConstByteLength = TEST_BYTES.length;50public static int nonConstObjectLength = TEST_OBJECTS.length;5152// Use this array to copy objects in.53public char[] dummyCharArray = new char[TEST_CHARS.length];54public byte[] dummyByteArray = new byte[TEST_BYTES.length];55public Object[] dummyObjectArray = new Object[TEST_OBJECTS.length];5657@Setup58public void setup() {59for (int i = 0; i < TEST_OBJECTS.length; i++) {60TEST_OBJECTS[i] = new Object();61dummyObjectArray[i] = new Object();62}63}6465/**66* This test case do the same work as testArrayCopy. We should make sure67* testArrayCopy is equally fast or better. Compare the two and you measure68* the system call versus explicit copy for-loop.69*/70@Benchmark71public void copyLoop() {72for (int j = 0; j < dummyByteArray.length; j++) {73dummyByteArray[j] = TEST_BYTES[j];74}75}7677/**78* Test that we can optimize away the code since it should not have any side79* effects80*/81@Benchmark82public void copyLoopLocalArray() {83byte[] localDummyByteArray = new byte[TEST_BYTES.length];84for (int j = 0; j < localDummyByteArray.length; j++) {85localDummyByteArray[j] = TEST_BYTES[j];86}87}8889/**90* This test case do the same work as testArrayCopy. We should make sure91* testArrayCopy is equally fast or better. Compare the two and you measure92* the system call versus explicit copy for-loop.93* <p/>94* Uses non-provable constant length.95*/96@Benchmark97public void copyLoopNonConst() {98for (int i = 0; i < nonConstByteLength; i++) {99dummyByteArray[i] = TEST_BYTES[i];100}101}102103/**104* This test case do the same work as testCopyLoop. We should make sure105* testArrayCopy is equally fast or better. Compare the two and you measure106* the system call versus explicit copy for-loop.107*/108@Benchmark109public void arrayCopy() {110System.arraycopy(TEST_BYTES, 0, dummyByteArray, 0, dummyByteArray.length);111}112113/**114* Test that we can optimize away the code since it should not have any side115* effects116*/117@Benchmark118public void arrayCopyLocalArray() {119byte[] localDummyByteArray = new byte[TEST_BYTES.length];120System.arraycopy(TEST_BYTES, 0, localDummyByteArray, 0, localDummyByteArray.length);121}122123/**124* This test case do the same work as testCopyLoop. We should make sure125* testArrayCopy is equally fast or better. Compare the two and you measure126* the system call versus explicit copy for-loop.127* <p/>128* Uses non-provable constant length.129*/130@Benchmark131public void arrayCopyNonConst() {132System.arraycopy(TEST_BYTES, 0, dummyByteArray, 0, nonConstByteLength);133}134135@Benchmark136public void arrayCopyChar() {137System.arraycopy(TEST_CHARS, 0, dummyCharArray, 0, dummyCharArray.length);138}139140@Benchmark141public void arrayCopyCharNonConst() {142System.arraycopy(TEST_CHARS, 0, dummyCharArray, 0, nonConstCharLength);143}144145@Benchmark146public void arrayCopyObject() {147System.arraycopy(TEST_OBJECTS, 0, dummyObjectArray, 0, dummyObjectArray.length);148}149150@Benchmark151public void arrayCopyObjectNonConst() {152System.arraycopy(TEST_OBJECTS, 0, dummyObjectArray, 0, nonConstObjectLength);153}154155/**156* This test copies inside a object array, that is same source array as dest157* array. Copies backwards in the array.158*/159@Benchmark160@OperationsPerInvocation(40)161public void arrayCopyObjectSameArraysBackward() {162for (int i = 0; i < 40; i++) {163System.arraycopy(dummyObjectArray, i, dummyObjectArray, i + 40, 80);164}165}166167/**168* This test copies inside a object array, that is same source array as dest169* array. Copies forward in the array. There is a special version for this170* in JRockit.171*/172@Benchmark173@OperationsPerInvocation(40)174public void arrayCopyObjectSameArraysForward() {175for (int i = 0; i < 40; i++) {176System.arraycopy(dummyObjectArray, i + 40, dummyObjectArray, i, 80);177}178}179}180181182