-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarking.java
More file actions
69 lines (57 loc) · 2.17 KB
/
Benchmarking.java
File metadata and controls
69 lines (57 loc) · 2.17 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
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.function.Consumer;
import java.util.stream.DoubleStream;
public class Benchmarking {
private int ENTRY_BOUND = 5_000;
private int MIN_N = 1_000;
private int MAX_N = 15_000;
private int DELTA_N = 500;
private int ITERS = 5; // number of repetitions per one arraySize
private Consumer<int[]> sortingMethod;
private String name;
private int[] data;
private Random random;
public Benchmarking(Consumer<int[]> sortingMethod, String name) {
this.sortingMethod = sortingMethod;
this.name = name;
random = new Random();
}
public Benchmarking(Consumer<int[]> sortingMethod, String name, int minN, int maxN, int deltaN, int iters) {
this(sortingMethod, name);
MIN_N = minN;
MAX_N = maxN;
DELTA_N = deltaN;
ITERS = iters;
}
public void run() {
try (PrintWriter out = new PrintWriter(new FileWriter(name + "-results.txt"))){
int resultsCount = (MAX_N - MIN_N) / DELTA_N + 1;
out.println(resultsCount);
for (int arraySize = MIN_N; arraySize <= MAX_N; arraySize += DELTA_N) {
out.print(arraySize + " ");
}
out.println();
for (int arraySize = MIN_N; arraySize <= MAX_N; arraySize += DELTA_N) {
final int N = arraySize; // new final variable to pass the in lambda
double avgElapsed = DoubleStream.generate(() -> timeTrial(N))
.limit(ITERS)
.average()
.getAsDouble();
out.print(avgElapsed + " ");
}
out.println();
} catch (IOException e) {}
}
private double timeTrial(int N) {
data = new int[N];
for (int i = 0; i < N; i++) {
data[i] = random.nextInt(ENTRY_BOUND);
}
Stopwatch timer = new Stopwatch();
sortingMethod.accept(data); // invoke sorting method
return timer.elapsedTime();
}
}