1 package ch.hslu.exercises.sw09;
2
3 import org.apache.commons.lang3.tuple.Pair;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6
7 import java.time.Duration;
8 import java.time.Instant;
9 import java.util.List;
10 import java.util.Random;
11 import java.util.function.Consumer;
12
13 public final class Sorting {
14 private List<Pair<String, Consumer<int[]>>> sortingAlgos;
15 private int[] sorted100;
16 private int[] sorted200;
17 private int[] sorted400;
18 private int[] reversed100;
19 private int[] reversed200;
20 private int[] reversed400;
21 private int[] random100;
22 private int[] random200;
23 private int[] random400;
24 private static final Logger LOG = LoggerFactory.getLogger(Sorting.class);
25
26 public Sorting(final List<Pair<String, Consumer<int[]>>> sortingAlgos) {
27 this.sortingAlgos = sortingAlgos;
28 initTestData();
29 }
30
31
32 public void test() {
33 for (Pair<String, Consumer<int[]>> sortingAlgo : sortingAlgos) {
34 Instant start;
35
36
37 start = Instant.now();
38 sortingAlgo.getRight().accept(sorted100);
39 Duration duration = Duration.between(start, Instant.now());
40 LOG.info(sortingAlgo.getLeft() + " with 100 000 sorted: " + duration);
41
42 start = Instant.now();
43 sortingAlgo.getRight().accept(sorted200);
44 duration = Duration.between(start, Instant.now());
45 LOG.info(sortingAlgo.getLeft() + " with 200 000 sorted: " + duration);
46
47 start = Instant.now();
48 sortingAlgo.getRight().accept(sorted400);
49 duration = Duration.between(start, Instant.now());
50 LOG.info(sortingAlgo.getLeft() + " with 400 000 sorted: " + duration);
51
52 start = Instant.now();
53 sortingAlgo.getRight().accept(reversed100);
54 duration = Duration.between(start, Instant.now());
55 LOG.info(sortingAlgo.getLeft() + " with 100 000 reversed: " + duration);
56
57 start = Instant.now();
58 sortingAlgo.getRight().accept(reversed200);
59 duration = Duration.between(start, Instant.now());
60 LOG.info(sortingAlgo.getLeft() + " with 200 000 reversed: " + duration);
61
62 start = Instant.now();
63 sortingAlgo.getRight().accept(reversed400);
64 duration = Duration.between(start, Instant.now());
65 LOG.info(sortingAlgo.getLeft() + " with 400 000 reversed: " + duration);
66
67 start = Instant.now();
68 sortingAlgo.getRight().accept(random100);
69 duration = Duration.between(start, Instant.now());
70 LOG.info(sortingAlgo.getLeft() + " with 100 000 random: " + duration);
71
72 start = Instant.now();
73 sortingAlgo.getRight().accept(random200);
74 duration = Duration.between(start, Instant.now());
75 LOG.info(sortingAlgo.getLeft() + " with 200 000 random: " + duration);
76
77 start = Instant.now();
78 sortingAlgo.getRight().accept(random400);
79 duration = Duration.between(start, Instant.now());
80 LOG.info(sortingAlgo.getLeft() + " with 400 000 random: " + duration);
81 initTestData();
82 }
83 }
84
85
86 private void initTestData() {
87 sorted100 = new int[100_000];
88 for (int i = 0; i < sorted100.length; i++) {
89 sorted100[i] = i;
90 }
91 sorted200 = new int[200_000];
92 for (int i = 0; i < sorted200.length; i++) {
93 sorted200[i] = i;
94 }
95 sorted400 = new int[400_000];
96 for (int i = 0; i < sorted400.length; i++) {
97 sorted400[i] = i;
98 }
99
100 reversed100 = new int[100_000];
101 int element = reversed100.length;
102 for (int i = 0; i < reversed100.length; i++) {
103 element--;
104 reversed100[i] = element;
105 }
106
107 reversed200 = new int[200_000];
108 element = reversed200.length;
109 for (int i = 0; i < reversed200.length; i++) {
110 element--;
111 reversed200[i] = element;
112 }
113
114 reversed400 = new int[400_000];
115 element = reversed400.length;
116 for (int i = 0; i < reversed400.length; i++) {
117 element--;
118 reversed400[i] = element;
119 }
120
121 Random random = new Random(0);
122 random100 = new int[100_000];
123 for (int i = 0; i < random100.length; i++) {
124 random100[i] = random.nextInt();
125 }
126 random200 = new int[200_000];
127 for (int i = 0; i < random200.length; i++) {
128 random200[i] = random.nextInt();
129 }
130 random400 = new int[400_000];
131 for (int i = 0; i < random400.length; i++) {
132 random400[i] = random.nextInt();
133 }
134 }
135
136 }