Path: blob/master/test/micro/org/openjdk/bench/java/math/BigIntegers.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.annotations.Param;33import org.openjdk.jmh.infra.Blackhole;3435import java.math.BigInteger;36import java.util.Random;37import java.util.concurrent.TimeUnit;3839@BenchmarkMode(Mode.AverageTime)40@OutputTimeUnit(TimeUnit.NANOSECONDS)41@State(Scope.Thread)42public class BigIntegers {4344private BigInteger[] hugeArray, largeArray, smallArray, shiftArray, smallShiftArray;45public String[] dummyStringArray;46public Object[] dummyArr;47private static final int TESTSIZE = 1000;4849@Param({"32", "64", "96", "128", "160", "192", "224", "256"})50private int maxNumbits;5152@Setup53public void setup() {54Random r = new Random(1123);55int numbits = r.nextInt(16384);5657hugeArray = new BigInteger[TESTSIZE]; /*58* Huge numbers larger than59* MAX_LONG60*/61largeArray = new BigInteger[TESTSIZE]; /*62* Large numbers less than63* MAX_LONG but larger than64* MAX_INT65*/66smallArray = new BigInteger[TESTSIZE]; /*67* Small number less than68* MAX_INT69*/70shiftArray = new BigInteger[TESTSIZE]; /*71* Each array entry is atmost 16k bits72* in size73*/74smallShiftArray = new BigInteger[TESTSIZE]; /*75* Small numbers, bits count in range [maxNumbits - 31, maxNumbits]76*/7778dummyStringArray = new String[TESTSIZE];79dummyArr = new Object[TESTSIZE];8081for (int i = 0; i < TESTSIZE; i++) {82int value = Math.abs(r.nextInt());8384hugeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE)85+ ((long) value + (long) Integer.MAX_VALUE));86largeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE));87smallArray[i] = new BigInteger("" + ((long) value / 1000));88shiftArray[i] = new BigInteger(numbits, r);89smallShiftArray[i] = new BigInteger(Math.max(maxNumbits - value % 32, 0), r);90}91}9293/** Test BigInteger.toString() with huge numbers larger than MAX_LONG */94@Benchmark95@OperationsPerInvocation(TESTSIZE)96public void testHugeToString(Blackhole bh) {97for (BigInteger s : hugeArray) {98bh.consume(s.toString());99}100}101102/** Test BigInteger.toString() with large numbers less than MAX_LONG but larger than MAX_INT */103@Benchmark104@OperationsPerInvocation(TESTSIZE)105public void testLargeToString(Blackhole bh) {106for (BigInteger s : largeArray) {107bh.consume(s.toString());108}109}110111/** Test BigInteger.toString() with small numbers less than MAX_INT */112@Benchmark113@OperationsPerInvocation(TESTSIZE)114public void testSmallToString(Blackhole bh) {115for (BigInteger s : smallArray) {116bh.consume(s.toString());117}118}119120/** Invokes the multiply method of BigInteger with various different values. */121@Benchmark122@OperationsPerInvocation(TESTSIZE)123public void testMultiply(Blackhole bh) {124BigInteger tmp = null;125for (BigInteger s : hugeArray) {126if (tmp == null) {127tmp = s;128continue;129}130tmp = tmp.multiply(s);131}132bh.consume(tmp);133}134135/** Invokes the multiply method of BigInteger with various different values. */136@Benchmark137@OperationsPerInvocation(TESTSIZE)138public void testAdd(Blackhole bh) {139BigInteger tmp = null;140for (BigInteger s : hugeArray) {141if (tmp == null) {142tmp = s;143continue;144}145tmp = tmp.add(s);146}147bh.consume(tmp);148}149150/** Invokes the shiftLeft method of BigInteger with different values. */151@Benchmark152@OperationsPerInvocation(TESTSIZE)153public void testLeftShift(Blackhole bh) {154Random rand = new Random();155int shift = rand.nextInt(30) + 1;156BigInteger tmp = null;157for (BigInteger s : shiftArray) {158if (tmp == null) {159tmp = s;160continue;161}162tmp = tmp.shiftLeft(shift);163}164bh.consume(tmp);165}166167/** Invokes the shiftRight method of BigInteger with different values. */168@Benchmark169@OperationsPerInvocation(TESTSIZE)170public void testRightShift(Blackhole bh) {171Random rand = new Random();172int shift = rand.nextInt(30) + 1;173BigInteger tmp = null;174for (BigInteger s : shiftArray) {175if (tmp == null) {176tmp = s;177continue;178}179tmp = tmp.shiftRight(shift);180}181bh.consume(tmp);182}183184/** Invokes the shiftLeft method of small BigInteger with different values. */185@Benchmark186@OperationsPerInvocation(TESTSIZE)187public void testSmallLeftShift(Blackhole bh) {188Random rand = new Random();189int shift = rand.nextInt(30) + 1;190BigInteger tmp = null;191for (BigInteger s : smallShiftArray) {192tmp = s.shiftLeft(shift);193bh.consume(tmp);194}195}196197/** Invokes the shiftRight method of small BigInteger with different values. */198@Benchmark199@OperationsPerInvocation(TESTSIZE)200public void testSmallRightShift(Blackhole bh) {201Random rand = new Random();202int shift = rand.nextInt(30) + 1;203BigInteger tmp = null;204for (BigInteger s : smallShiftArray) {205tmp = s.shiftRight(shift);206bh.consume(tmp);207}208}209}210211212