Skip to content

Commit

Permalink
more performance tests for quadratic residue computations
Browse files Browse the repository at this point in the history
  • Loading branch information
TilmanNeumann committed Jan 1, 2025
1 parent 3980604 commit 8a5a0f8
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package de.tilman_neumann.jml.quadraticResidues;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

import org.apache.logging.log4j.Logger;
Expand All @@ -23,15 +22,13 @@
import de.tilman_neumann.util.ConfigUtil;

/**
* Performance tests of quadratic residue computations modulo 2^n.
* Test performance of quadratic residue computations modulo 2^n.
*
* @author Tilman Neumann
*/
public class QuadraticResiduesMod2PowNPerformanceTest {

private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNPerformanceTest.class);

private static final boolean SHOW_ELEMENTS = false;

/**
* Test.
Expand All @@ -47,28 +44,28 @@ public static void main(String[] args) {
t0 = System.currentTimeMillis();
List<BigInteger> quadraticResiduesMod2PowN_v0 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_big(n);
t1 = System.currentTimeMillis();
LOG.info("v0: n = " + n + " has " + quadraticResiduesMod2PowN_v0.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v0 : "") + " -- duration = " + (t1-t0) + "ms");
LOG.info("v0: n = " + n + ": Computed " + quadraticResiduesMod2PowN_v0.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
}

if (n<29) { // avoid OutOfMemoryError
t0 = System.currentTimeMillis();
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll(n);
t1 = System.currentTimeMillis();
LOG.info("v1: n = " + n + " has " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v1 : "") + " -- duration = " + (t1-t0) + "ms");
LOG.info("v1: n = " + n + ": Computed " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
}

if (n<31) { // avoid OutOfMemoryError
t0 = System.currentTimeMillis();
List<Long> quadraticResiduesMod2PowN_v2 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_v2(n);
t1 = System.currentTimeMillis();
LOG.info("v2: n = " + n + " has " + quadraticResiduesMod2PowN_v2.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v2 : "") + " -- duration = " + (t1-t0) + "ms");
LOG.info("v2: n = " + n + ": Computed " + quadraticResiduesMod2PowN_v2.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
}

if (n<30) { // avoid OutOfMemoryError
t0 = System.currentTimeMillis();
List<Long> quadraticResiduesMod2PowN_v3 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n);
t1 = System.currentTimeMillis();
LOG.info("v3: n = " + n + " has " + quadraticResiduesMod2PowN_v3.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v3 : "") + " -- duration = " + (t1-t0) + "ms");
LOG.info("v3: n = " + n + ": Computed " + quadraticResiduesMod2PowN_v3.size() + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
}

if (n<33) { // avoid OutOfMemoryError
Expand All @@ -77,7 +74,7 @@ public static void main(String[] args) {
long[] array = new long[arraySize];
int count = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n, array);
t1 = System.currentTimeMillis();
LOG.info("v4: n = " + n + " has " + count + " quadratic residues" + (SHOW_ELEMENTS ? ": " + Arrays.toString(array) : "") + " -- duration = " + (t1-t0) + "ms");
LOG.info("v4: n = " + n + ": Computed " + count + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*/
package de.tilman_neumann.jml.quadraticResidues;

import java.util.List;
import java.util.TreeSet;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import de.tilman_neumann.util.ConfigUtil;

/**
* Test performance of quadratic residue computations modulo 3^n.
*
* @author Tilman Neumann
*/
public class QuadraticResiduesMod3PowNPerformanceTest {

private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod3PowNPerformanceTest.class);

/**
* Test.
* @param args ignored
*/
public static void main(String[] args) {
ConfigUtil.initProject();

for (int n=0; n<17; n++) {
long t0, t1;
long m = (long) Math.pow(3, n);

t0 = System.currentTimeMillis();
TreeSet<Long> quadraticResiduesModPow = QuadraticResidues.getQuadraticResidues(m);
t1 = System.currentTimeMillis();
LOG.info("v1: n = " + n + ": Computed " + quadraticResiduesModPow.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");

t0 = System.currentTimeMillis();
List<Long> quadraticResiduesModPow_v2 = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN(n);
t1 = System.currentTimeMillis();
LOG.info("v2: n = " + n + ": Computed " + quadraticResiduesModPow_v2.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");

t0 = System.currentTimeMillis();
List<Long> quadraticResiduesModPow_v3 = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN_testAll(n);
t1 = System.currentTimeMillis();
LOG.info("v3: n = " + n + ": Computed " + quadraticResiduesModPow_v3.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*/
package de.tilman_neumann.jml.quadraticResidues;

import java.util.List;
import java.util.TreeSet;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import de.tilman_neumann.util.ConfigUtil;

/**
* Test performance of quadratic residue computations modulo p^n.
*
* @author Tilman Neumann
*/
public class QuadraticResiduesModBPowNPerformanceTest {

private static final Logger LOG = LogManager.getLogger(QuadraticResiduesModBPowNPerformanceTest.class);

/**
* Test.
* @param args ignored
*/
private static void testPerformance(int p, int nMax) {
ConfigUtil.initProject();

for (int n=0; n<=nMax; n++) {
long m = (long) Math.pow(p, n);

long t0, t1;
t0 = System.currentTimeMillis();
TreeSet<Long> quadraticResiduesModPow = QuadraticResidues.getQuadraticResidues(m);
t1 = System.currentTimeMillis();
LOG.info("v1: n = " + n + ": Computed " + quadraticResiduesModPow.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");

t0 = System.currentTimeMillis();
List<Long> quadraticResiduesModPow_v2 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN(p, n);
t1 = System.currentTimeMillis();
LOG.info("v2: n = " + n + ": Computed " + quadraticResiduesModPow_v2.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");

t0 = System.currentTimeMillis();
List<Long> quadraticResiduesModPow_v3 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN_testAll(p, n);
t1 = System.currentTimeMillis();
LOG.info("v3: n = " + n + ": Computed " + quadraticResiduesModPow_v3.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");

t0 = System.currentTimeMillis();
List<Long> quadraticResiduesModPow_v4 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN_testAll_v2(p, n);
t1 = System.currentTimeMillis();
LOG.info("v4: n = " + n + ": Computed " + quadraticResiduesModPow_v4.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
}
}

/**
* Test.
* @param args ignored
*/
public static void main(String[] args) {
ConfigUtil.initProject();
testPerformance(3, 16);
testPerformance(5, 11);
testPerformance(7, 9);
}
}

0 comments on commit 8a5a0f8

Please sign in to comment.