-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculateFactorialWithTiming.java
68 lines (53 loc) · 2.65 KB
/
CalculateFactorialWithTiming.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package MultiThreading;
import java.math.BigInteger;
public class CalculateFactorialWithTiming {
public static void main(String[] args) throws InterruptedException {
long programStartTime = System.currentTimeMillis(); // Record the start time of the program
int[] numbers = {10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 35000};
System.out.println("Number of processors: " + Runtime.getRuntime().availableProcessors());
MyThread[] threads = new MyThread[numbers.length];
long[] executionTimes = new long[numbers.length];
long maxExecutionTime = 0;
for (int i = 0; i < numbers.length; i++) {
threads[i] = new MyThread(numbers[i]);
threads[i].start();
}
for (int i = 0; i < numbers.length; i++) {
threads[i].join(); // Wait for the thread to die
long executionTime = threads[i].getExecutionTime();
executionTimes[i] = executionTime;
//to print the result - factorial calculated for each elements
// System.out.println(threads[i].result);
System.out.println("Factorial of " + numbers[i] + " took " + executionTime + " milliseconds.");
// Update the maximum execution time
maxExecutionTime = Math.max(executionTime,maxExecutionTime);
}
long programEndTime = System.currentTimeMillis(); // Record the end time of the program
long totalProgramExecutionTime = programEndTime - programStartTime;// Total execution time for program
System.out.println("Total program execution time: " + totalProgramExecutionTime + " milliseconds");
System.out.println("Maximum execution time of thread : " + maxExecutionTime + " milliseconds");
}
private static class MyThread extends Thread {
private int num;
private BigInteger result;
private long executionTime;
MyThread(int num) {
this.num = num;
this.result = BigInteger.ONE;
}
public void run() {
long startTime = System.currentTimeMillis();//Record the start time of a single thread
calculate();
long endTime = System.currentTimeMillis();//Record the end time of a single thread
executionTime = endTime - startTime;//Total execution time of a single thread
}
public long getExecutionTime() {
return executionTime;
}
public void calculate() {
for (int i = 2; i <= num; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
}
}
}