hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.micro.benchmarks/src/micro/benchmarks/MathFunctionBenchmark.java
author iveresov
Fri, 11 Aug 2017 17:36:26 -0700
changeset 46807 8b2c620d7092
permissions -rw-r--r--
8186158: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46807
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     1
/*
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     4
 *
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     8
 *
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    14
 *
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    18
 *
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    22
 */
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    23
package micro.benchmarks;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    24
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    25
import java.util.Random;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    26
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    27
import org.openjdk.jmh.annotations.Benchmark;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    28
import org.openjdk.jmh.annotations.Scope;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    29
import org.openjdk.jmh.annotations.State;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    30
import org.openjdk.jmh.annotations.Warmup;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    31
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    32
/**
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    33
 * Benchmarks cost of Math intrinsics.
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    34
 */
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    35
public class MathFunctionBenchmark extends BenchmarkBase {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    36
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    37
    @State(Scope.Benchmark)
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    38
    public static class ThreadState {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    39
        double[] data = randomDoubles(100);
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    40
        double[] result = new double[100];
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    41
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    42
        static double[] randomDoubles(int len) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    43
            double[] data = new double[len];
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    44
            Random r = new Random();
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    45
            for (int i = 0; i < data.length; i++) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    46
                data[i] = r.nextDouble();
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    47
            }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    48
            return data;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    49
        }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    50
    }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    51
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    52
    @Benchmark
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    53
    @Warmup(iterations = 5)
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    54
    public void mathLog(ThreadState state) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    55
        double[] data = state.data;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    56
        for (int i = 0; i < data.length; i++) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    57
            double[] result = state.result;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    58
            result[i] = Math.log(data[i]);
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    59
        }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    60
    }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    61
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    62
    @Benchmark
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    63
    @Warmup(iterations = 5)
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    64
    public void mathLog10(ThreadState state) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    65
        double[] data = state.data;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    66
        for (int i = 0; i < data.length; i++) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    67
            double[] result = state.result;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    68
            result[i] = Math.log10(data[i]);
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    69
        }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    70
    }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    71
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    72
    @Benchmark
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    73
    @Warmup(iterations = 5)
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    74
    public void mathSin(ThreadState state) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    75
        double[] data = state.data;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    76
        for (int i = 0; i < data.length; i++) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    77
            double[] result = state.result;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    78
            result[i] = Math.sin(data[i]);
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    79
        }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    80
    }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    81
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    82
    @Benchmark
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    83
    @Warmup(iterations = 5)
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    84
    public void mathCos(ThreadState state) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    85
        double[] data = state.data;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    86
        for (int i = 0; i < data.length; i++) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    87
            double[] result = state.result;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    88
            result[i] = Math.cos(data[i]);
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    89
        }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    90
    }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    91
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    92
    @Benchmark
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    93
    @Warmup(iterations = 5)
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    94
    public void mathTan(ThreadState state) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    95
        double[] data = state.data;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    96
        for (int i = 0; i < data.length; i++) {
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    97
            double[] result = state.result;
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    98
            result[i] = Math.tan(data[i]);
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
    99
        }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
   100
    }
8b2c620d7092 8186158: Update Graal
iveresov
parents:
diff changeset
   101
}