summaryrefslogtreecommitdiff
path: root/benchmarks/SmallBigIntegerBenchmark.java
blob: f513bf43a12ab35b29d3df041266b3bda1492bff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright (C) 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package benchmarks;

import java.math.BigInteger;
import java.util.Random;

/**
 * This measures performance of operations on small BigIntegers.
 * We manually determine the number of iterations so that it should cause total memory
 * allocation on the order of a few hundred megabytes.  Due to BigInteger's reliance on
 * finalization, these may unfortunately all be kept around at once.
 *
 * This is not structured as a proper benchmark; just run main(), e.g. with
 * vogar libcore/benchmarks/src/benchmarks/SmallBigIntegerBenchmark.java
 */
public class SmallBigIntegerBenchmark {
    // We allocate about 2 1/3 BigIntegers per iteration.
    // Assuming 100 bytes/BigInteger, this gives us around 500MB total.
    static final int NITERS = 2 * 1000 * 1000;
    static final BigInteger BIG_THREE = BigInteger.valueOf(3);
    static final BigInteger BIG_FOUR = BigInteger.valueOf(4);

    public static void main(String args[]) {
        final Random r = new Random();
        BigInteger x = new BigInteger(20, r);
        final long startNanos = System.nanoTime();
        long intermediateNanos = 0;
        for (int i = 0; i < NITERS; ++i) {
            if (i == NITERS / 100) {
                intermediateNanos = System.nanoTime();
            }
            // We know this converges, but the compiler doesn't.
            if (x.and(BigInteger.ONE).equals(BigInteger.ONE)) {
                x = x.multiply(BIG_THREE).add(BigInteger.ONE);
            } else {
                x = x.shiftRight(1);
            }
        }
        if (x.signum() < 0 || x.compareTo(BIG_FOUR) > 0) {
            throw new AssertionError("Something went horribly wrong.");
        }
        final long finalNanos = System.nanoTime();
        double firstFewTime = ((double) intermediateNanos - (double) startNanos) / (NITERS / 100);
        double restTime = ((double) finalNanos - (double) intermediateNanos) / (99 * NITERS / 100);
        System.out.println("First Few: " + firstFewTime
                + " nanoseconds per iteration (2.33 BigInteger ops/iter)");
        System.out.println("Remainder: " + restTime + " nanoseconds per iteration");
    }
}