Path: blob/master/test/micro/org/openjdk/bench/java/lang/ArrayFiddle.java
41161 views
/*1* Copyright 2019 Google Inc. 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.*;2526import java.util.Arrays;27import java.util.concurrent.ThreadLocalRandom;28import java.util.concurrent.TimeUnit;2930/**31* Explores the cost of copying the contents of one array to another, as is32* commonly seen in collection classes that need to resize their backing33* array, like ArrayList.34*35* We have multiple variations on copying, and we explore the cost of36* clearing the old array, which might help for generational GCs.37*38* Benchmarks the operations in the ancient39* JDK-6428387: array clone() much slower than Arrays.copyOf40*41* 2019 results on x86:42*43* The "simple" benchmarks below have the same performance, except that44* simple_copyLoop is surprisingly 5x slower. The array copying intrinsics45* are very effective and a naive loop does not get optimized the same way.46* OTOH there is no intrinsic for Arrays.fill but the naive array zeroing loop47* *does* get optimized to something a little faster than than arraycopy.48*49* System.arraycopy and Arrays.fill have such outstanding performance that50* one should use them to replace handwritten loops whenever possible.51*52* This benchmark is great for measuring cache effects, e.g. size=10^6 has 5x53* the per-element cost of size=10^3 (See "The Myth of RAM".)54*55* (cd $(git rev-parse --show-toplevel) && for size in 3 16 999 999999; do make test TEST='micro:java.lang.ArrayFiddle' MICRO="FORK=2;WARMUP_ITER=4;ITER=4;OPTIONS=-opi $size -p size=$size" |& perl -ne 'print if /^Benchmark/ .. /^Finished running test/'; done)56*/57@BenchmarkMode(Mode.AverageTime)58@Fork(2)59@Warmup(iterations = 1)60@Measurement(iterations = 4)61@OutputTimeUnit(TimeUnit.NANOSECONDS)62@State(Scope.Benchmark)63public class ArrayFiddle {64@Param("999")65public int size;6667public int largerSize;68public Object[] data;69public Object[] copy;7071@Setup72public void setup() {73largerSize = size + (size >> 1);7475data = new Object[size];76ThreadLocalRandom rnd = ThreadLocalRandom.current();77for (int i = data.length; i--> 0; )78data[i] = rnd.nextInt(256);7980copy = data.clone();81}8283// --- "simple" benchmarks just make an array clone8485@Benchmark86public Object[] simple_clone() {87return data.clone();88}8990@Benchmark91public Object[] simple_copyOf() {92return Arrays.copyOf(data, data.length);93}9495@Benchmark96public Object[] simple_arraycopy() {97Object[] out = new Object[data.length];98System.arraycopy(data, 0, out, 0, data.length);99return out;100}101102@Benchmark103public Object[] simple_copyLoop() {104final Object[] data = this.data;105int len = data.length;106Object[] out = new Object[len];107for (int i = 0; i < len; i++)108out[i] = data[i];109return out;110}111112// --- "grow" benchmarks have an output array that is larger113114private Object[] input_array() {115System.arraycopy(data, 0, copy, 0, size);116return copy;117}118119@Benchmark120public Object[] grow_copyLoop() {121Object[] in = input_array();122Object[] out = new Object[largerSize];123for (int i = 0, len = in.length; i < len; i++)124out[i] = in[i];125return out;126}127128@Benchmark129public Object[] grow_copyZeroLoop() {130Object[] in = input_array();131Object[] out = new Object[largerSize];132for (int i = 0, len = in.length; i < len; i++) {133out[i] = in[i];134in[i] = null;135}136return out;137}138139@Benchmark140public Object[] grow_arraycopy() {141Object[] in = input_array();142Object[] out = new Object[largerSize];143System.arraycopy(in, 0, out, 0, size);144return out;145}146147@Benchmark148public Object[] grow_arraycopy_fill() {149Object[] in = input_array();150Object[] out = new Object[largerSize];151System.arraycopy(in, 0, out, 0, size);152Arrays.fill(in, null);153return out;154}155156@Benchmark157public Object[] grow_arraycopy_zeroLoop() {158Object[] in = input_array();159Object[] out = new Object[largerSize];160System.arraycopy(in, 0, out, 0, size);161for (int i = 0, len = in.length; i < len; i++)162in[i] = null;163return out;164}165166@Benchmark167public Object[] grow_copyOf() {168Object[] in = input_array();169Object[] out = Arrays.copyOf(in, largerSize);170return out;171}172173@Benchmark174public Object[] grow_copyOf_fill() {175Object[] in = input_array();176Object[] out = Arrays.copyOf(in, largerSize);177Arrays.fill(in, null);178return out;179}180181@Benchmark182public Object[] grow_copyOf_zeroLoop() {183Object[] in = input_array();184Object[] out = Arrays.copyOf(in, largerSize);185for (int i = 0, len = in.length; i < len; i++)186in[i] = null;187return out;188}189190}191192193