Skip to content

Commit 8a5a0f8

Browse files
committed
more performance tests for quadratic residue computations
1 parent 3980604 commit 8a5a0f8

File tree

3 files changed

+142
-9
lines changed

3 files changed

+142
-9
lines changed

src/test/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod2PowNPerformanceTest.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package de.tilman_neumann.jml.quadraticResidues;
1515

1616
import java.math.BigInteger;
17-
import java.util.Arrays;
1817
import java.util.List;
1918

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

2524
/**
26-
* Performance tests of quadratic residue computations modulo 2^n.
25+
* Test performance of quadratic residue computations modulo 2^n.
2726
*
2827
* @author Tilman Neumann
2928
*/
3029
public class QuadraticResiduesMod2PowNPerformanceTest {
3130

3231
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNPerformanceTest.class);
33-
34-
private static final boolean SHOW_ELEMENTS = false;
3532

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

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

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

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

7471
if (n<33) { // avoid OutOfMemoryError
@@ -77,7 +74,7 @@ public static void main(String[] args) {
7774
long[] array = new long[arraySize];
7875
int count = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n, array);
7976
t1 = System.currentTimeMillis();
80-
LOG.info("v4: n = " + n + " has " + count + " quadratic residues" + (SHOW_ELEMENTS ? ": " + Arrays.toString(array) : "") + " -- duration = " + (t1-t0) + "ms");
77+
LOG.info("v4: n = " + n + ": Computed " + count + " quadratic residues modulo 2^" + n + " in " + (t1-t0) + "ms");
8178
}
8279
}
8380
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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-2024 Tilman Neumann - [email protected]
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.quadraticResidues;
15+
16+
import java.util.List;
17+
import java.util.TreeSet;
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 quadratic residue computations modulo 3^n.
26+
*
27+
* @author Tilman Neumann
28+
*/
29+
public class QuadraticResiduesMod3PowNPerformanceTest {
30+
31+
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod3PowNPerformanceTest.class);
32+
33+
/**
34+
* Test.
35+
* @param args ignored
36+
*/
37+
public static void main(String[] args) {
38+
ConfigUtil.initProject();
39+
40+
for (int n=0; n<17; n++) {
41+
long t0, t1;
42+
long m = (long) Math.pow(3, n);
43+
44+
t0 = System.currentTimeMillis();
45+
TreeSet<Long> quadraticResiduesModPow = QuadraticResidues.getQuadraticResidues(m);
46+
t1 = System.currentTimeMillis();
47+
LOG.info("v1: n = " + n + ": Computed " + quadraticResiduesModPow.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
48+
49+
t0 = System.currentTimeMillis();
50+
List<Long> quadraticResiduesModPow_v2 = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN(n);
51+
t1 = System.currentTimeMillis();
52+
LOG.info("v2: n = " + n + ": Computed " + quadraticResiduesModPow_v2.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
53+
54+
t0 = System.currentTimeMillis();
55+
List<Long> quadraticResiduesModPow_v3 = QuadraticResiduesMod3PowN.getQuadraticResiduesMod3PowN_testAll(n);
56+
t1 = System.currentTimeMillis();
57+
LOG.info("v3: n = " + n + ": Computed " + quadraticResiduesModPow_v3.size() + " quadratic residues mod 3^" + n + " in " + (t1-t0) + "ms");
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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-2024 Tilman Neumann - [email protected]
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.quadraticResidues;
15+
16+
import java.util.List;
17+
import java.util.TreeSet;
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 quadratic residue computations modulo p^n.
26+
*
27+
* @author Tilman Neumann
28+
*/
29+
public class QuadraticResiduesModBPowNPerformanceTest {
30+
31+
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesModBPowNPerformanceTest.class);
32+
33+
/**
34+
* Test.
35+
* @param args ignored
36+
*/
37+
private static void testPerformance(int p, int nMax) {
38+
ConfigUtil.initProject();
39+
40+
for (int n=0; n<=nMax; n++) {
41+
long m = (long) Math.pow(p, n);
42+
43+
long t0, t1;
44+
t0 = System.currentTimeMillis();
45+
TreeSet<Long> quadraticResiduesModPow = QuadraticResidues.getQuadraticResidues(m);
46+
t1 = System.currentTimeMillis();
47+
LOG.info("v1: n = " + n + ": Computed " + quadraticResiduesModPow.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
48+
49+
t0 = System.currentTimeMillis();
50+
List<Long> quadraticResiduesModPow_v2 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN(p, n);
51+
t1 = System.currentTimeMillis();
52+
LOG.info("v2: n = " + n + ": Computed " + quadraticResiduesModPow_v2.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
53+
54+
t0 = System.currentTimeMillis();
55+
List<Long> quadraticResiduesModPow_v3 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN_testAll(p, n);
56+
t1 = System.currentTimeMillis();
57+
LOG.info("v3: n = " + n + ": Computed " + quadraticResiduesModPow_v3.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
58+
59+
t0 = System.currentTimeMillis();
60+
List<Long> quadraticResiduesModPow_v4 = QuadraticResiduesModBPowN.getQuadraticResiduesModBPowN_testAll_v2(p, n);
61+
t1 = System.currentTimeMillis();
62+
LOG.info("v4: n = " + n + ": Computed " + quadraticResiduesModPow_v4.size() + " quadratic residues mod " + p + "^" + n + " in " + (t1-t0) + "ms");
63+
}
64+
}
65+
66+
/**
67+
* Test.
68+
* @param args ignored
69+
*/
70+
public static void main(String[] args) {
71+
ConfigUtil.initProject();
72+
testPerformance(3, 16);
73+
testPerformance(5, 11);
74+
testPerformance(7, 9);
75+
}
76+
}

0 commit comments

Comments
 (0)