-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPiExperimentRunner.java
More file actions
70 lines (55 loc) · 2.8 KB
/
PiExperimentRunner.java
File metadata and controls
70 lines (55 loc) · 2.8 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
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
69
70
public class PiExperimentRunner {
public static void main(String[] args) {
// --- Configuration ---
long[] pointConfigs = {100_000L, 1_000_000L, 10_000_000L};
int[] threadConfigs = {2, 4, 8};
int tasks = 100;
System.out.printf("%-15s %-10s %-12s %-12s %-15s %-15s%n",
"Type", "Threads", "Points", "Time(ms)", "Estimate", "Error");
System.out.println("-------------------------------------------------------------------------------------");
// 1. Run Sequential
PiEstimator sequential = new SequentialPiEstimator();
for (long n : pointConfigs) {
runExperiment("Sequential", sequential, new SimulationConfig(n, 1, 1));
}
System.out.println("-------------------------------------------------------------------------------------");
// 2. Run Parallel
PiEstimator parallel = new ParallelPiEstimator();
for (long n : pointConfigs) {
for (int t : threadConfigs) {
runExperiment("Parallel", parallel, new SimulationConfig(n, tasks, t));
}
}
System.out.println("-------------------------------------------------------------------------------------");
// 3. Bonus: Batch Experiments
System.out.println("BONUS: Running Batch Experiments (Averaging 10 runs)");
SimulationConfig batchConfig = new SimulationConfig(1_000_000L, 20, 4);
runBatchExperiments(new ParallelPiEstimator(), batchConfig, 10);
}
// --- Helper Methods ---
private static void runExperiment(String type, PiEstimator estimator, SimulationConfig config) {
long startTime = System.currentTimeMillis();
double piEstimate = estimator.estimatePi(config);
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
double error = Math.abs(piEstimate - Math.PI);
System.out.printf("%-15s %-10d %-12d %-12d %-15.8f %-15.8f%n",
type, config.getNumThreads(), config.getTotalPoints(), duration, piEstimate, error);
}
private static void runBatchExperiments(PiEstimator estimator, SimulationConfig config, int trials) {
double totalPi = 0;
double totalTime = 0;
for (int i = 0; i < trials; i++) {
long start = System.currentTimeMillis();
double result = estimator.estimatePi(config);
long time = System.currentTimeMillis() - start;
totalPi += result;
totalTime += time;
}
double avgPi = totalPi / trials;
double avgTime = totalTime / trials;
double avgError = Math.abs(avgPi - Math.PI);
System.out.printf(" > Batch Result (%d trials): Avg Time: %.2f ms | Avg Pi: %.7f | Avg Error: %.7f%n",
trials, avgTime, avgPi, avgError);
}
}