|
| 1 | +/* |
| 2 | + * java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project. |
| 3 | + * Copyright (C) 2018-2026 Tilman Neumann - tilman.neumann@web.de |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 9 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public License along with this program; |
| 12 | + * if not, see <http://www.gnu.org/licenses/>. |
| 13 | + */ |
| 14 | +package de.tilman_neumann.jml.modular; |
| 15 | + |
| 16 | +import java.math.BigInteger; |
| 17 | +import java.util.Random; |
| 18 | + |
| 19 | +import org.apache.logging.log4j.Logger; |
| 20 | +import org.apache.logging.log4j.LogManager; |
| 21 | + |
| 22 | +import de.tilman_neumann.util.ConfigUtil; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test performance of modular power implementations. |
| 26 | + * |
| 27 | + * My implementation with long modulus is 4 times slower than BigInteger :-( |
| 28 | + * But at least the version with int modulus is faster. |
| 29 | + */ |
| 30 | +public class ModularPowerPerformanceTest { |
| 31 | + private static final Logger LOG = LogManager.getLogger(ModularPowerPerformanceTest.class); |
| 32 | + |
| 33 | + private static final int NCOUNT = 100000; |
| 34 | + |
| 35 | + private static final Random RNG = new Random(); |
| 36 | + private static final ModularPower modPow = new ModularPower(); |
| 37 | + |
| 38 | + private static void testPerformance() { |
| 39 | + |
| 40 | + // set up test numbers |
| 41 | + |
| 42 | + long[] a_arr = new long[NCOUNT]; |
| 43 | + long[] b_arr = new long[NCOUNT]; |
| 44 | + long[] c_arr = new long[NCOUNT]; |
| 45 | + int[] cInt_arr = new int[NCOUNT]; |
| 46 | + BigInteger[] aBig_arr = new BigInteger[NCOUNT]; |
| 47 | + BigInteger[] bBig_arr = new BigInteger[NCOUNT]; |
| 48 | + BigInteger[] cBig_arr = new BigInteger[NCOUNT]; |
| 49 | + BigInteger[] cIntBig_arr = new BigInteger[NCOUNT]; |
| 50 | + |
| 51 | + for (int i=0; i<NCOUNT; i++) { |
| 52 | + a_arr[i] = getPositiveRandomLong(); |
| 53 | + b_arr[i] = getPositiveRandomLong(); |
| 54 | + c_arr[i] = getPositiveRandomLong(); |
| 55 | + cInt_arr[i] = (int) (c_arr[i] & Integer.MAX_VALUE); |
| 56 | + |
| 57 | + aBig_arr[i] = BigInteger.valueOf(a_arr[i]); |
| 58 | + bBig_arr[i] = BigInteger.valueOf(b_arr[i]); |
| 59 | + cBig_arr[i] = BigInteger.valueOf(c_arr[i]); |
| 60 | + cIntBig_arr[i] = BigInteger.valueOf(cInt_arr[i]); |
| 61 | + } |
| 62 | + |
| 63 | + long t0, t1; |
| 64 | + |
| 65 | + // test BigInteger with long modulus |
| 66 | + t0 = System.currentTimeMillis(); |
| 67 | + for (int i=0; i<NCOUNT; i++) { |
| 68 | + aBig_arr[i].modPow(bBig_arr[i], cBig_arr[i]); |
| 69 | + } |
| 70 | + t1 = System.currentTimeMillis(); |
| 71 | + LOG.info("BigInteger.modPow(long modulus) took " + (t1-t0) + " ms"); |
| 72 | + |
| 73 | + // test my implementation with long modulus |
| 74 | + t0 = System.currentTimeMillis(); |
| 75 | + for (int i=0; i<NCOUNT; i++) { |
| 76 | + modPow.modPow(a_arr[i], b_arr[i], c_arr[i]); |
| 77 | + } |
| 78 | + t1 = System.currentTimeMillis(); |
| 79 | + LOG.info("ModularPower.modPow(long modulus) took " + (t1-t0) + " ms"); |
| 80 | + |
| 81 | + // test BigInteger with int modulus |
| 82 | + t0 = System.currentTimeMillis(); |
| 83 | + for (int i=0; i<NCOUNT; i++) { |
| 84 | + aBig_arr[i].modPow(bBig_arr[i], cIntBig_arr[i]); |
| 85 | + } |
| 86 | + t1 = System.currentTimeMillis(); |
| 87 | + LOG.info("BigInteger.modPow(long modulus) took " + (t1-t0) + " ms"); |
| 88 | + |
| 89 | + // test my implementation with int modulus |
| 90 | + t0 = System.currentTimeMillis(); |
| 91 | + for (int i=0; i<NCOUNT; i++) { |
| 92 | + modPow.modPow(a_arr[i], b_arr[i], cInt_arr[i]); |
| 93 | + } |
| 94 | + t1 = System.currentTimeMillis(); |
| 95 | + LOG.info("ModularPower.modPow(int modulus) took " + (t1-t0) + " ms"); |
| 96 | + } |
| 97 | + |
| 98 | + private static long getPositiveRandomLong() { |
| 99 | + long n = RNG.nextLong(); |
| 100 | + while (n == 0) n = RNG.nextLong(); |
| 101 | + return Math.abs(n); |
| 102 | + } |
| 103 | + |
| 104 | + public static void main(String[] args) { |
| 105 | + ConfigUtil.initProject(); |
| 106 | + testPerformance(); |
| 107 | + } |
| 108 | +} |
0 commit comments