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 LongMath}.
31  *
32  * @author Louis Wasserman
33  */
34 public class LongMathBenchmark {
35   private static final int[] exponents = new int[ARRAY_SIZE];
36   private static final int[] factorialArguments = new int[ARRAY_SIZE];
37   private static final int[][] binomialArguments = new int[ARRAY_SIZE][2];
38   private static final long[] positive = new long[ARRAY_SIZE];
39   private static final long[] nonnegative = new long[ARRAY_SIZE];
40   private static final long[] longs = new long[ARRAY_SIZE];
41 
42   @BeforeExperiment
setUp()43   void setUp() {
44     for (int i = 0; i < ARRAY_SIZE; i++) {
45       exponents[i] = randomExponent();
46       positive[i] = randomPositiveBigInteger(Long.SIZE - 1).longValue();
47       nonnegative[i] = randomNonNegativeBigInteger(Long.SIZE - 1).longValue();
48       longs[i] = RANDOM_SOURCE.nextLong();
49       factorialArguments[i] = RANDOM_SOURCE.nextInt(30);
50       binomialArguments[i][1] = RANDOM_SOURCE.nextInt(MathBenchmarking.biggestBinomials.length);
51       int k = binomialArguments[i][1];
52       binomialArguments[i][0] = RANDOM_SOURCE.nextInt(MathBenchmarking.biggestBinomials[k] - k) + k;
53     }
54   }
55 
56   @Benchmark
pow(int reps)57   int pow(int reps) {
58     int tmp = 0;
59     for (int i = 0; i < reps; i++) {
60       int j = i & ARRAY_MASK;
61       tmp += LongMath.pow(positive[j], exponents[j]);
62     }
63     return tmp;
64   }
65 
66   @Benchmark
mod(int reps)67   int mod(int reps) {
68     int tmp = 0;
69     for (int i = 0; i < reps; i++) {
70       int j = i & ARRAY_MASK;
71       tmp += LongMath.mod(longs[j], positive[j]);
72     }
73     return tmp;
74   }
75 
76   @Benchmark
gCD(int reps)77   int gCD(int reps) {
78     int tmp = 0;
79     for (int i = 0; i < reps; i++) {
80       int j = i & ARRAY_MASK;
81       tmp += LongMath.mod(nonnegative[j], positive[j]);
82     }
83     return tmp;
84   }
85 
86   @Benchmark
factorial(int reps)87   int factorial(int reps) {
88     int tmp = 0;
89     for (int i = 0; i < reps; i++) {
90       int j = i & ARRAY_MASK;
91       tmp += LongMath.factorial(factorialArguments[j]);
92     }
93     return tmp;
94   }
95 
96   @Benchmark
binomial(int reps)97   int binomial(int reps) {
98     int tmp = 0;
99     for (int i = 0; i < reps; i++) {
100       int j = i & ARRAY_MASK;
101       tmp += LongMath.binomial(binomialArguments[j][0], binomialArguments[j][1]);
102     }
103     return tmp;
104   }
105 
106   @Benchmark
isPrime(int reps)107   int isPrime(int reps) {
108     int tmp = 0;
109     for (int i = 0; i < reps; i++) {
110       int j = i & ARRAY_MASK;
111       if (LongMath.isPrime(positive[j])) {
112         tmp++;
113       }
114     }
115     return tmp;
116   }
117 }
118