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 import com.google.common.math.IntMath; 29 30 /** 31 * Benchmarks for the non-rounding methods of {@code IntMath}. 32 * 33 * @author Louis Wasserman 34 */ 35 public class IntMathBenchmark { 36 private static int[] exponent = new int[ARRAY_SIZE]; 37 private static int[] factorial = new int[ARRAY_SIZE]; 38 private static int[] binomial = new int[ARRAY_SIZE]; 39 private static final int[] positive = new int[ARRAY_SIZE]; 40 private static final int[] nonnegative = new int[ARRAY_SIZE]; 41 private static final int[] ints = new int[ARRAY_SIZE]; 42 43 @BeforeExperiment 44 void setUp() { 45 for (int i = 0; i < ARRAY_SIZE; i++) { 46 exponent[i] = randomExponent(); 47 factorial[i] = RANDOM_SOURCE.nextInt(50); 48 binomial[i] = RANDOM_SOURCE.nextInt(factorial[i] + 1); 49 positive[i] = randomPositiveBigInteger(Integer.SIZE - 1).intValue(); 50 nonnegative[i] = randomNonNegativeBigInteger(Integer.SIZE - 1).intValue(); 51 ints[i] = RANDOM_SOURCE.nextInt(); 52 } 53 } 54 55 @Benchmark 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 int mod(int reps) { 65 int tmp = 0; 66 for (int i = 0; i < reps; i++) { 67 int j = i & ARRAY_MASK; 68 tmp += IntMath.mod(ints[j], positive[j]); 69 } 70 return tmp; 71 } 72 73 @Benchmark int gCD(int reps) { 74 int tmp = 0; 75 for (int i = 0; i < reps; i++) { 76 int j = i & ARRAY_MASK; 77 tmp += IntMath.gcd(nonnegative[j], positive[j]); 78 } 79 return tmp; 80 } 81 82 @Benchmark int factorial(int reps) { 83 int tmp = 0; 84 for (int i = 0; i < reps; i++) { 85 int j = i & ARRAY_MASK; 86 tmp += IntMath.factorial(factorial[j]); 87 } 88 return tmp; 89 } 90 91 @Benchmark int binomial(int reps) { 92 int tmp = 0; 93 for (int i = 0; i < reps; i++) { 94 int j = i & ARRAY_MASK; 95 tmp += IntMath.binomial(factorial[j], binomial[j]); 96 } 97 return tmp; 98 } 99 } 100