/* * Copyright (c) BELLSOFT. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package org.openjdk.bench.java.lang; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.OperationsPerInvocation; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.Level; import java.lang.Double; import java.lang.Math; import java.util.Arrays; import java.util.Random; import java.util.concurrent.TimeUnit; @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @State(Scope.Thread) public class DoubleSignum { double[] mostlyZero, mostlyNan, mostlyPos, mostlyNeg, random; final int TESTSIZE = 1000; // cover all branches static void prefixMixed(double[] data) { data[0] = 0.0d; data[1] = Double.NaN; data[2] = 2.39d; data[3] = -2.39d; } @Setup(Level.Iteration) public void generateData() { mostlyZero = new double[TESTSIZE]; prefixMixed(mostlyZero); mostlyNan = new double[TESTSIZE]; Arrays.fill(mostlyNan, Double.NaN); prefixMixed(mostlyNan); mostlyPos = new double[TESTSIZE]; Arrays.fill(mostlyPos, 2.39f); prefixMixed(mostlyPos); mostlyNeg = new double[TESTSIZE]; Arrays.fill(mostlyNeg, -2.39f); prefixMixed(mostlyNeg); Random r = new Random(); random = new double[TESTSIZE]; for(int i = 0; i < TESTSIZE; i++) random[i] = r.nextDouble() - 0.5d; prefixMixed(random); } @Benchmark @OperationsPerInvocation(TESTSIZE) public void ofMostlyNaN(Blackhole bh) { for(double d : mostlyNan) bh.consume(Math.signum(d)); } @Benchmark @OperationsPerInvocation(TESTSIZE) public void ofMostlyZero(Blackhole bh) { for(double d : mostlyZero) bh.consume(Math.signum(d)); } @Benchmark @OperationsPerInvocation(TESTSIZE) public void ofMostlyPos(Blackhole bh) { for(double d : mostlyPos) bh.consume(Math.signum(d)); } @Benchmark @OperationsPerInvocation(TESTSIZE) public void ofMostlyNeg(Blackhole bh) { for(double d : mostlyNeg) bh.consume(Math.signum(d)); } @Benchmark @OperationsPerInvocation(TESTSIZE) public void ofRandom(Blackhole bh) { for(double d : random) bh.consume(Math.signum(d)); } @Benchmark @OperationsPerInvocation(TESTSIZE) public void overhead(Blackhole bh) { for (double d : random) bh.consume(d); } }