1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.math; 18 19 import static com.google.common.math.MathBenchmarking.ARRAY_MASK; 20 import static com.google.common.math.MathBenchmarking.ARRAY_SIZE; 21 import static com.google.common.math.MathBenchmarking.RANDOM_SOURCE; 22 import static com.google.common.math.MathBenchmarking.randomExponent; 23 import static com.google.common.math.MathBenchmarking.randomNonNegativeBigInteger; 24 import static com.google.common.math.MathBenchmarking.randomPositiveBigInteger; 25 26 import com.google.caliper.BeforeExperiment; 27 import com.google.caliper.Benchmark; 28 29 /** 30 * Benchmarks for the non-rounding methods of {@code IntMath}. 31 * 32 * @author Louis Wasserman 33 */ 34 public class IntMathBenchmark { 35 private static int[] exponent = new int[ARRAY_SIZE]; 36 private static int[] factorial = new int[ARRAY_SIZE]; 37 private static int[] binomial = new int[ARRAY_SIZE]; 38 private static final int[] positive = new int[ARRAY_SIZE]; 39 private static final int[] nonnegative = new int[ARRAY_SIZE]; 40 private static final int[] ints = new int[ARRAY_SIZE]; 41 42 @BeforeExperiment setUp()43 void setUp() { 44 for (int i = 0; i < ARRAY_SIZE; i++) { 45 exponent[i] = randomExponent(); 46 factorial[i] = RANDOM_SOURCE.nextInt(50); 47 binomial[i] = RANDOM_SOURCE.nextInt(factorial[i] + 1); 48 positive[i] = randomPositiveBigInteger(Integer.SIZE - 1).intValue(); 49 nonnegative[i] = randomNonNegativeBigInteger(Integer.SIZE - 1).intValue(); 50 ints[i] = RANDOM_SOURCE.nextInt(); 51 } 52 } 53 54 @Benchmark pow(int reps)55 int pow(int reps) { 56 int tmp = 0; 57 for (int i = 0; i < reps; i++) { 58 int j = i & ARRAY_MASK; 59 tmp += IntMath.pow(positive[j], exponent[j]); 60 } 61 return tmp; 62 } 63 64 @Benchmark mod(int reps)65 int mod(int reps) { 66 int tmp = 0; 67 for (int i = 0; i < reps; i++) { 68 int j = i & ARRAY_MASK; 69 tmp += IntMath.mod(ints[j], positive[j]); 70 } 71 return tmp; 72 } 73 74 @Benchmark gCD(int reps)75 int gCD(int reps) { 76 int tmp = 0; 77 for (int i = 0; i < reps; i++) { 78 int j = i & ARRAY_MASK; 79 tmp += IntMath.gcd(nonnegative[j], positive[j]); 80 } 81 return tmp; 82 } 83 84 @Benchmark factorial(int reps)85 int factorial(int reps) { 86 int tmp = 0; 87 for (int i = 0; i < reps; i++) { 88 int j = i & ARRAY_MASK; 89 tmp += IntMath.factorial(factorial[j]); 90 } 91 return tmp; 92 } 93 94 @Benchmark binomial(int reps)95 int binomial(int reps) { 96 int tmp = 0; 97 for (int i = 0; i < reps; i++) { 98 int j = i & ARRAY_MASK; 99 tmp += IntMath.binomial(factorial[j], binomial[j]); 100 } 101 return tmp; 102 } 103 104 @Benchmark isPrime(int reps)105 int isPrime(int reps) { 106 int tmp = 0; 107 for (int i = 0; i < reps; i++) { 108 int j = i & ARRAY_MASK; 109 if (IntMath.isPrime(positive[j])) { 110 tmp++; 111 } 112 } 113 return tmp; 114 } 115 } 116