Path: blob/master/test/micro/org/openjdk/bench/java/math/BigDecimals.java
41161 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. 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.math;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OperationsPerInvocation;28import org.openjdk.jmh.annotations.OutputTimeUnit;29import org.openjdk.jmh.annotations.Scope;30import org.openjdk.jmh.annotations.Setup;31import org.openjdk.jmh.annotations.State;32import org.openjdk.jmh.infra.Blackhole;3334import java.math.BigDecimal;35import java.math.RoundingMode;36import java.util.Random;37import java.util.concurrent.TimeUnit;3839@BenchmarkMode(Mode.AverageTime)40@OutputTimeUnit(TimeUnit.NANOSECONDS)41@State(Scope.Thread)42public class BigDecimals {4344/** Make sure TEST_SIZE is used to size the arrays. We need this constant to parametrize the operations count. */45private static final int TEST_SIZE = 100;4647/* dummy variables for intermediate results */48public Object[] dummyArr;49public String[] dummyStringArray;50public int dummy;5152/* array to hold the created objects. */53private BigDecimal[] bigDecimals;54private String[] stringInputs;55private double[] doubleInputs;56private BigDecimal[] hugeArray, largeArray, smallArray;5758@Setup59public void setup() {60Random r = new Random(1123);61dummyArr = new Object[TEST_SIZE];62bigDecimals = new BigDecimal[TEST_SIZE];63stringInputs = new String[TEST_SIZE];64doubleInputs = new double[TEST_SIZE];65for (int i = 0; i < TEST_SIZE; i++) {66double value = (double) (i + 1);67switch (i % 4) {68case 0:69value = -value * 54345.0d;70break;71case 1:72value = value * 5434543453454355e100;73break;74case 2:75value = -value / 5434543453454355e100;76break;77case 3:78break;79}8081bigDecimals[i] = new BigDecimal(value);82stringInputs[i] = "" + value;83doubleInputs[i] = value;84}8586/*87* Huge numbers larger than MAX_LONG88*/89hugeArray = new BigDecimal[TEST_SIZE];9091/*92* Large numbers less than MAX_LONG but larger than MAX_INT93*/94largeArray = new BigDecimal[TEST_SIZE];9596/*97* Small number less than MAX_INT98*/99smallArray = new BigDecimal[TEST_SIZE];100101dummyStringArray = new String[TEST_SIZE];102for (int i = 0; i < TEST_SIZE; i++) {103int value = Math.abs(r.nextInt());104hugeArray[i] = new BigDecimal("" + ((long) value + (long) Integer.MAX_VALUE)105+ ((long) value + (long) Integer.MAX_VALUE) + ".55");106largeArray[i] = new BigDecimal("" + ((long) value + (long) Integer.MAX_VALUE) + ".55");107smallArray[i] = new BigDecimal("" + ((long) value / 1000) + ".55");108}109}110111/** Invokes the (String)-constructor of BigDecimal with various different values. */112@Benchmark113@OperationsPerInvocation(TEST_SIZE)114public void testConstructorWithString(Blackhole bh) {115for (String s : stringInputs) {116bh.consume(new BigDecimal(s));117}118}119120/** Invokes the (double)-constructor of BigDecimal with various different values. */121@Benchmark122@OperationsPerInvocation(TEST_SIZE)123public void testConstructorWithDouble(Blackhole bh) {124for (double s : doubleInputs) {125bh.consume(new BigDecimal(s));126}127}128129/** Invokes the toString method of BigDecimal with various different values. */130@Benchmark131@OperationsPerInvocation(TEST_SIZE)132public void testToString(Blackhole bh) {133for (BigDecimal s : bigDecimals) {134bh.consume(s.toString());135}136}137138/**139* Invokes the setScale method of BigDecimal with various different values.140*/141@Benchmark142@OperationsPerInvocation(TEST_SIZE)143public void testSetScale(Blackhole bh) {144for (BigDecimal s : bigDecimals) {145bh.consume(s.setScale(2, RoundingMode.HALF_UP));146}147}148149/** Invokes the setScale method of BigDecimal with various different values. */150@Benchmark151@OperationsPerInvocation(50 * TEST_SIZE)152public void testSetScaleVarious(Blackhole bh) {153for (int scale = 0; scale < 50; scale++) {154for (BigDecimal s : bigDecimals) {155bh.consume(s.setScale(scale, RoundingMode.HALF_UP));156}157}158}159160/** Invokes the add method of BigDecimal with various different values. */161@Benchmark162@OperationsPerInvocation(TEST_SIZE)163public void testAdd(Blackhole bh) {164BigDecimal tmp = null;165for (BigDecimal s : bigDecimals) {166if (tmp == null) {167tmp = s;168continue;169}170tmp = tmp.add(s);171}172bh.consume(tmp);173}174175/** Invokes the multiply method of BigDecimal with various different values. */176@Benchmark177@OperationsPerInvocation(TEST_SIZE)178public void testMultiply(Blackhole bh) {179BigDecimal tmp = null;180for (BigDecimal s : bigDecimals) {181if (tmp == null) {182tmp = s;183continue;184}185tmp = tmp.multiply(s);186}187bh.consume(tmp);188}189190/** Invokes the compareTo method of BigDecimal with various different values. */191@Benchmark192@OperationsPerInvocation(TEST_SIZE - 1)193public void testCompareTo(Blackhole bh) {194BigDecimal c = bigDecimals[0];195for (BigDecimal s : bigDecimals) {196bh.consume(c.compareTo(s));197}198}199200/** Test BigDecimal.toString() with huge numbers larger than MAX_LONG */201@Benchmark202@OperationsPerInvocation(TEST_SIZE)203public void testHugeToString(Blackhole bh) {204for (BigDecimal s : hugeArray) {205bh.consume(s.toString());206}207}208209/** Test BigDecimal.toString() with large numbers less than MAX_LONG but larger than MAX_INT */210@Benchmark211@OperationsPerInvocation(TEST_SIZE)212public void testLargeToString(Blackhole bh) {213for (BigDecimal s : largeArray) {214bh.consume(s.toString());215}216}217218/** Test BigDecimal.toString() with small numbers less than MAX_INT */219@Benchmark220@OperationsPerInvocation(TEST_SIZE)221public void testSmallToString(Blackhole bh) {222for (BigDecimal s : smallArray) {223bh.consume(s.toString());224}225}226}227228229