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