1 /* 2 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 /* 26 * To change this template, choose Tools | Templates 27 * and open the template in the editor. 28 */ 29 package org.openjdk.bench.java.math; 30 31 import org.openjdk.jmh.annotations.Benchmark; 32 import org.openjdk.jmh.annotations.BenchmarkMode; 33 import org.openjdk.jmh.annotations.Mode; 34 import org.openjdk.jmh.annotations.OperationsPerInvocation; 35 import org.openjdk.jmh.annotations.OutputTimeUnit; 36 import org.openjdk.jmh.annotations.Scope; 37 import org.openjdk.jmh.annotations.Setup; 38 import org.openjdk.jmh.annotations.State; 39 import org.openjdk.jmh.infra.Blackhole; 40 41 import java.math.BigInteger; 42 import java.util.Random; 43 import java.util.concurrent.TimeUnit; 44 45 @BenchmarkMode(Mode.AverageTime) 46 @OutputTimeUnit(TimeUnit.NANOSECONDS) 47 @State(Scope.Thread) 48 public class BigIntegers { 49 50 private BigInteger[] hugeArray, largeArray, smallArray; 51 public String[] dummyStringArray; 52 public Object[] dummyArr; 53 private static final int TESTSIZE = 1000; 54 55 @Setup 56 public void setup() { 57 Random r = new Random(1123); 58 59 hugeArray = new BigInteger[TESTSIZE]; /* 60 * Huge numbers larger than 61 * MAX_LONG 62 */ 63 largeArray = new BigInteger[TESTSIZE]; /* 64 * Large numbers less than 65 * MAX_LONG but larger than 66 * MAX_INT 67 */ 68 smallArray = new BigInteger[TESTSIZE]; /* 69 * Small number less than 70 * MAX_INT 71 */ 72 73 dummyStringArray = new String[TESTSIZE]; 74 dummyArr = new Object[TESTSIZE]; 75 76 for (int i = 0; i < TESTSIZE; i++) { 77 int value = Math.abs(r.nextInt()); 78 79 hugeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE) 80 + ((long) value + (long) Integer.MAX_VALUE)); 81 largeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE)); 82 smallArray[i] = new BigInteger("" + ((long) value / 1000)); 83 } 84 } 85 86 /** Test BigInteger.toString() with huge numbers larger than MAX_LONG */ 87 @Benchmark 88 @OperationsPerInvocation(TESTSIZE) 89 public void testHugeToString(Blackhole bh) { 90 for (BigInteger s : hugeArray) { 91 bh.consume(s.toString()); 92 } 93 } 94 95 /** Test BigInteger.toString() with large numbers less than MAX_LONG but larger than MAX_INT */ 96 @Benchmark 97 @OperationsPerInvocation(TESTSIZE) 98 public void testLargeToString(Blackhole bh) { 99 for (BigInteger s : largeArray) { 100 bh.consume(s.toString()); 101 } 102 } 103 104 /** Test BigInteger.toString() with small numbers less than MAX_INT */ 105 @Benchmark 106 @OperationsPerInvocation(TESTSIZE) 107 public void testSmallToString(Blackhole bh) { 108 for (BigInteger s : smallArray) { 109 bh.consume(s.toString()); 110 } 111 } 112 113 /** Invokes the multiply method of BigInteger with various different values. */ 114 @Benchmark 115 @OperationsPerInvocation(TESTSIZE) 116 public void testMultiply(Blackhole bh) { 117 BigInteger tmp = null; 118 for (BigInteger s : hugeArray) { 119 if (tmp == null) { 120 tmp = s; 121 continue; 122 } 123 tmp = tmp.multiply(s); 124 } 125 bh.consume(tmp); 126 } 127 128 /** Invokes the multiply method of BigInteger with various different values. */ 129 @Benchmark 130 @OperationsPerInvocation(TESTSIZE) 131 public void testAdd(Blackhole bh) { 132 BigInteger tmp = null; 133 for (BigInteger s : hugeArray) { 134 if (tmp == null) { 135 tmp = s; 136 continue; 137 } 138 tmp = tmp.add(s); 139 } 140 bh.consume(tmp); 141 } 142 }