-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAddition.java
More file actions
40 lines (36 loc) · 1.6 KB
/
Copy pathTestAddition.java
File metadata and controls
40 lines (36 loc) · 1.6 KB
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
import java.math.BigInteger;
import java.util.Random;
public class TestAddition {
public static void main(String[] args) {
Random random = new Random();
System.out.println("Size, Time(ns)");
System.out.println("8, "+time(8,random));
System.out.println("16, "+time(16, random));
System.out.println("64, "+time(64,random));
System.out.println("128, "+time(128,random));
System.out.println("512, "+time(512,random));
System.out.println("1024, "+time(1024,random));;
}
public static double time (int numBits,Random random) {
StopWatch watch = new StopWatch().reset().start();
int nTimes = 0;
int a = 0;
watch.stop().reset();
while (watch.elapsed() < 0.01 || nTimes < 2) {
// Setup a problem
/** This constructor is more suitable as it allows us to generate random BigIntegers with a specific bit
length (numBits). This constructor offers flexibility in creating BigIntegers with different bit lengths
thus being more controlled.
*/
BigInteger num1 = new BigInteger(numBits, random);
BigInteger num2 = new BigInteger(numBits, random);
// Run a problem - Measure the time for addition
watch.start();
BigInteger result = num1.add(num2);
watch.stop();
nTimes++;
}
return watch.elapsed() / nTimes ;
}
}
// Oracle Java documentation for the BigInteger class - https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html