1 /* 2 * Copyright (C) 2013 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package benchmarks; 18 19 import com.google.caliper.Param; 20 import com.google.caliper.SimpleBenchmark; 21 22 public class SystemArrayCopyBenchmark extends SimpleBenchmark { 23 @Param({"2", "4", "8", "16", "32", "64", "128", "256", "512", "1024", 24 "2048", "4096", "8192", "16384", "32768", "65536", "131072", "262144"}) 25 int arrayLength; 26 27 // Provides benchmarking for different types of arrays using the arraycopy function. 28 timeSystemCharArrayCopy(int reps)29 public void timeSystemCharArrayCopy(int reps) { 30 final int len = arrayLength; 31 char[] src = new char[len]; 32 char[] dst = new char[len]; 33 for (int rep = 0; rep < reps; ++rep) { 34 System.arraycopy(src, 0, dst, 0, len); 35 } 36 } 37 timeSystemByteArrayCopy(int reps)38 public void timeSystemByteArrayCopy(int reps) { 39 final int len = arrayLength; 40 byte[] src = new byte[len]; 41 byte[] dst = new byte[len]; 42 for (int rep = 0; rep < reps; ++rep) { 43 System.arraycopy(src, 0, dst, 0, len); 44 } 45 } 46 timeSystemShortArrayCopy(int reps)47 public void timeSystemShortArrayCopy(int reps) { 48 final int len = arrayLength; 49 short[] src = new short[len]; 50 short[] dst = new short[len]; 51 for (int rep = 0; rep < reps; ++rep) { 52 System.arraycopy(src, 0, dst, 0, len); 53 } 54 } 55 timeSystemIntArrayCopy(int reps)56 public void timeSystemIntArrayCopy(int reps) { 57 final int len = arrayLength; 58 int[] src = new int[len]; 59 int[] dst = new int[len]; 60 for (int rep = 0; rep < reps; ++rep) { 61 System.arraycopy(src, 0, dst, 0, len); 62 } 63 } 64 timeSystemLongArrayCopy(int reps)65 public void timeSystemLongArrayCopy(int reps) { 66 final int len = arrayLength; 67 long[] src = new long[len]; 68 long[] dst = new long[len]; 69 for (int rep = 0; rep < reps; ++rep) { 70 System.arraycopy(src, 0, dst, 0, len); 71 } 72 } 73 timeSystemFloatArrayCopy(int reps)74 public void timeSystemFloatArrayCopy(int reps) { 75 final int len = arrayLength; 76 float[] src = new float[len]; 77 float[] dst = new float[len]; 78 for (int rep = 0; rep < reps; ++rep) { 79 System.arraycopy(src, 0, dst, 0, len); 80 } 81 } 82 timeSystemDoubleArrayCopy(int reps)83 public void timeSystemDoubleArrayCopy(int reps) { 84 final int len = arrayLength; 85 double[] src = new double[len]; 86 double[] dst = new double[len]; 87 for (int rep = 0; rep < reps; ++rep) { 88 System.arraycopy(src, 0, dst, 0, len); 89 } 90 } 91 timeSystemBooleanArrayCopy(int reps)92 public void timeSystemBooleanArrayCopy(int reps) { 93 final int len = arrayLength; 94 boolean[] src = new boolean[len]; 95 boolean[] dst = new boolean[len]; 96 for (int rep = 0; rep < reps; ++rep) { 97 System.arraycopy(src, 0, dst, 0, len); 98 } 99 } 100 } 101