Newer
Older
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
71
package collection;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Benchmark {
private static final Random RNG = new Random(12345);
public static void main(String[] args) throws Exception {
int[] ns = { 1000, 2000, 5000, 10000, 20000, 50000 }; // adapte
int reps = 5; // répétitions pour moyenne
String outFile = "benchmark_results.csv";
try (PrintWriter pw = new PrintWriter(new FileWriter(outFile))) {
pw.println("structure,scenario,n,phase,avg_millis");
for (String structure : Arrays.asList("ABR", "ARN")) {
for (String scenario : Arrays.asList("random", "ascending")) {
for (int n : ns) {
long insertSum = 0, searchSum = 0;
for (int r = 0; r < reps; r++) {
List<Integer> keys = new ArrayList<>();
for (int i = 0; i < n; i++)
keys.add(i);
if (scenario.equals("random"))
Collections.shuffle(keys, new Random(RNG.nextLong()));
// choose structure
Collection<Integer> tree = structure.equals("ARN") ? new RedBlackTree<>() : new BinarySearchTree<>();
long t0 = System.nanoTime();
for (Integer k : keys)
tree.add(k);
long t1 = System.nanoTime();
long insertMs = (t1 - t0);
insertSum += insertMs;
// search phase: 0 .. 2n-1
long t2 = System.nanoTime();
for (int x = 0; x < 2 * n; x++) {
tree.contains(x);
}
long t3 = System.nanoTime();
long searchMs = (t3 - t2);
searchSum += searchMs;
}
pw.printf("%s,%s,%d,insert,%.2f%n", structure, scenario, n, insertSum / (double) reps);
pw.printf("%s,%s,%d,search,%.2f%n", structure, scenario, n, searchSum / (double) reps);
pw.flush();
System.out.printf("Done %s %s n=%d%n", structure, scenario, n);
}
}
}
}
System.out.println("Wrote results to " + outFile);
}
}