|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2020, Arm Limited. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.openjdk.bench.vm.compiler; |
| 26 | + |
| 27 | +import org.openjdk.jmh.annotations.Benchmark; |
| 28 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 29 | +import org.openjdk.jmh.annotations.Mode; |
| 30 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Param; |
| 32 | +import org.openjdk.jmh.annotations.Scope; |
| 33 | +import org.openjdk.jmh.annotations.Setup; |
| 34 | +import org.openjdk.jmh.annotations.State; |
| 35 | + |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.Arrays; |
| 38 | + |
| 39 | +@State(Scope.Benchmark) |
| 40 | +@BenchmarkMode(Mode.AverageTime) |
| 41 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 42 | +public class ArrayFill { |
| 43 | + @Param("65536") private int size; |
| 44 | + |
| 45 | + private byte[] ba; |
| 46 | + private short[] sa; |
| 47 | + private int[] ia; |
| 48 | + |
| 49 | + @Setup |
| 50 | + public void setup() { |
| 51 | + ba = new byte[size]; |
| 52 | + sa = new short[size]; |
| 53 | + ia = new int[size]; |
| 54 | + } |
| 55 | + |
| 56 | + @Benchmark |
| 57 | + public void fillByteArray() { |
| 58 | + for (int i = 0; i < size; i++) { |
| 59 | + ba[i] = (byte) 123; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Benchmark |
| 64 | + public void fillShortArray() { |
| 65 | + for (int i = 0; i < size; i++) { |
| 66 | + sa[i] = (short) 12345; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Benchmark |
| 71 | + public void fillIntArray() { |
| 72 | + for (int i = 0; i < size; i++) { |
| 73 | + ia[i] = 1234567890; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Benchmark |
| 78 | + public void zeroByteArray() { |
| 79 | + for (int i = 0; i < size; i++) { |
| 80 | + ba[i] = 0; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Benchmark |
| 85 | + public void zeroShortArray() { |
| 86 | + for (int i = 0; i < size; i++) { |
| 87 | + sa[i] = 0; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Benchmark |
| 92 | + public void zeroIntArray() { |
| 93 | + for (int i = 0; i < size; i++) { |
| 94 | + ia[i] = 0; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments