com.github.tell.arithmetic.integer.gmp.MPZBenchmarking.java Source code

Java tutorial

Introduction

Here is the source code for com.github.tell.arithmetic.integer.gmp.MPZBenchmarking.java

Source

/**
 * @author Tadanori TERUYA <tadanori.teruya@gmail.com> (2013)
 * @license: The MIT license <http://opensource.org/licenses/MIT>
 */
/*
 * Copyright (c) 2013 Tadanori TERUYA (tell) <tadanori.teruya@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * @license: The MIT license <http://opensource.org/licenses/MIT>
 */
package com.github.tell.arithmetic.integer.gmp;

import org.apache.commons.lang3.time.StopWatch;
import org.apache.log4j.PropertyConfigurator;

import java.math.BigInteger;
import java.security.SecureRandom;

/**
 * @author Tadanori TERUYA &lt;tadanori.teruya@gmail.com&gt; (2013)
 */
public class MPZBenchmarking {

    static {
        PropertyConfigurator.configure("log4j.properties");
    }

    static public final int numOfLoop = 1000;
    private static SecureRandom secureRandom;

    public static void printout(final StopWatch timer, final int length, final String name) {
        final double scale = 1e3;
        final String scaleUnit = "us";
        final double nanoPart = (((double) timer.getNanoTime()) / scale) / numOfLoop;
        System.out.printf("% 10.03f [%s]: %s bits mul. over %s%n", nanoPart, scaleUnit, length, name);
    }

    public static void printOutRatio(final StopWatch a, final StopWatch b, final String message) {
        final double ratio = ((double) a.getNanoTime()) / ((double) b.getNanoTime());
        System.out.printf("% 10.03f: %s%n", ratio, message);
    }

    @SuppressWarnings("UnusedAssignment")
    private static StopWatch mulWithBigInteger(final int length) {
        final BigInteger x1 = new BigInteger(length, secureRandom);
        final BigInteger x2 = new BigInteger(length, secureRandom);

        @SuppressWarnings("unused")
        BigInteger x3 = BigInteger.valueOf(0);
        final StopWatch timer = new StopWatch();
        timer.start();
        for (int j = 0; j < numOfLoop; j++) {
            x3 = x1.multiply(x2);
        }
        timer.stop();

        printout(timer, length, "BigInteger");

        return timer;
    }

    private static StopWatch mulWithMPZ(final int length) {
        final MPZ x1 = new MPZ(secureRandom, length);
        final MPZ x2 = new MPZ(secureRandom, length);

        final MPZ x3 = new MPZ(0);
        final StopWatch timer = new StopWatch();
        timer.start();
        for (int j = 0; j < numOfLoop; j++) {
            MPZ.mul(x3, x1, x2);
        }
        timer.stop();

        printout(timer, length, "MPZ");

        return timer;
    }

    private static void benchMul() {
        final int first = 1 << 10;
        final int step = 1 << 10;
        final int times = 1 << 4;

        System.out.println("----");
        for (int i = 0; i < times; i++) {
            final StopWatch benchBI = mulWithBigInteger(first + step * i);
            final StopWatch benchMPZ = mulWithMPZ(first + step * i);
            printOutRatio(benchBI, benchMPZ, "BigInteger / MPZ");
            System.out.println("----");
        }
    }

    public static void main(String[] args) {
        secureRandom = new SecureRandom();
        benchMul();
    }
}