Path: blob/master/test/micro/org/openjdk/bench/vm/compiler/MaxMinOptimizeTest.java
41161 views
/*1* Copyright (c) 2021, Huawei Technologies Co. Ltd. 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.vm.compiler;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.*;2627import java.util.Random;28import java.util.concurrent.TimeUnit;29import org.openjdk.jmh.infra.Blackhole;3031@BenchmarkMode({Mode.AverageTime})32@OutputTimeUnit(TimeUnit.MICROSECONDS)33@State(Scope.Thread)34public class MaxMinOptimizeTest {35private static final int COUNT = 100000;3637private float[] floats_a = new float[COUNT];38private float[] floats_b = new float[COUNT];39private double[] doubles_a = new double[COUNT];40private double[] doubles_b = new double[COUNT];4142private Random r = new Random();4344@Setup45public void init() {46for (int i=0; i<COUNT; i++) {47floats_a[i] = r.nextFloat();48floats_b[i] = r.nextFloat();49doubles_a[i] = r.nextDouble();50doubles_b[i] = r.nextDouble();51}52}5354@Benchmark55public void fAdd(Blackhole bh) {56float sum = 0;57for (int i=0; i<COUNT; i++)58sum += fAddBench(floats_a[i], floats_b[i]);59bh.consume(sum);60}6162@Benchmark63public void fMul(Blackhole bh) {64float sum = 0;65for (int i=0; i<COUNT; i++)66sum += fMulBench(floats_a[i], floats_b[i]);67bh.consume(sum);68}6970@Benchmark71public void fMax(Blackhole bh) {72float sum = 0;73for (int i=0; i<COUNT; i++)74sum += fMaxBench(floats_a[i], floats_b[i]);75bh.consume(sum);76}7778@Benchmark79public void fMin(Blackhole bh) {80float sum = 0;81for (int i=0; i<COUNT; i++)82sum += fMinBench(floats_a[i], floats_b[i]);83bh.consume(sum);84}8586private float fAddBench(float a, float b) {87return Math.max(a, b) + Math.min(a, b);88}8990private float fMulBench(float a, float b) {91return Math.max(a, b) * Math.min(a, b);92}9394private float fMaxBench(float a, float b) {95return Math.max(Math.max(a, b), Math.min(a, b));96}9798private float fMinBench(float a, float b) {99return Math.min(Math.max(a, b), Math.min(a, b));100}101102103@Benchmark104public void dAdd(Blackhole bh) {105double sum = 0;106for (int i=0; i<COUNT; i++)107sum += dAddBench(doubles_a[i], doubles_b[i]);108bh.consume(sum);109}110111@Benchmark112public void dMul(Blackhole bh) {113double sum = 0;114for (int i=0; i<COUNT; i++)115sum += dMulBench(doubles_a[i], doubles_b[i]);116bh.consume(sum);117}118119@Benchmark120public void dMax(Blackhole bh) {121double sum = 0;122for (int i=0; i<COUNT; i++)123sum += dMaxBench(doubles_a[i], doubles_b[i]);124bh.consume(sum);125}126127@Benchmark128public void dMin(Blackhole bh) {129double sum = 0;130for (int i=0; i<COUNT; i++)131sum += dMinBench(doubles_a[i], doubles_b[i]);132bh.consume(sum);133}134135private double dAddBench(double a, double b) {136return Math.max(a, b) + Math.min(a, b);137}138139private double dMulBench(double a, double b) {140return Math.max(a, b) * Math.min(a, b);141}142143private double dMaxBench(double a, double b) {144return Math.max(Math.max(a, b), Math.min(a, b));145}146147private double dMinBench(double a, double b) {148return Math.min(Math.max(a, b), Math.min(a, b));149}150}151152153