Skip to content

Commit 309126d

Browse files
committed
Changed location for generating testData. Theoretically 3x speed increase
1 parent 2c86fea commit 309126d

2 files changed

Lines changed: 31 additions & 33 deletions

File tree

TreeTimeComplexity/src/main/java/main/BenchmarkResultGUI.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package main;
1818

19+
import algorithms.IntQuickSort;
1920
import utils.Operations;
2021
import algorithms.MinBinaryHeap;
2122
import algorithms.MinBinomialHeap;
@@ -44,6 +45,7 @@ public class BenchmarkResultGUI {
4445
private int iterations;
4546
private Operations[] operations;
4647
private InputTypes sorting;
48+
private int[] benchData;
4749

4850
/**
4951
* Class for creating barchart window
@@ -58,6 +60,8 @@ public BenchmarkResultGUI(int inputLenght, int iterations, Operations[] operatio
5860
this.iterations = iterations;
5961
this.operations = operations;
6062
this.sorting = sorting;
63+
//Create test data for bechmarks
64+
this.benchData = createTestData(iterations);
6165

6266
}
6367

@@ -84,9 +88,9 @@ public Scene run() {
8488
series3.setName("Fibonacci Heap");
8589
ObservableList<Label> list = FXCollections.<Label>observableArrayList();
8690

87-
Benchmark benBinary = new Benchmark(new MinBinaryHeap(), this.iterations, this.inputLenght, this.sorting);
88-
Benchmark benBinomial = new Benchmark(new MinBinomialHeap(), this.iterations, this.inputLenght, this.sorting);
89-
Benchmark benFibonacci = new Benchmark(new MinFibonaciHeap(), this.iterations, this.inputLenght, this.sorting);
91+
Benchmark benBinary = new Benchmark(new MinBinaryHeap(), this.iterations, this.benchData);
92+
Benchmark benBinomial = new Benchmark(new MinBinomialHeap(), this.iterations, this.benchData);
93+
Benchmark benFibonacci = new Benchmark(new MinFibonaciHeap(), this.iterations, this.benchData);
9094

9195
for (Operations operation : operations) {
9296

@@ -172,4 +176,25 @@ public Scene run() {
172176
return chartScene;
173177
}
174178

179+
private int[] createTestData(int size) {
180+
181+
int[] values = new int[size];
182+
183+
for (int i = 0; i < size; i++) {
184+
185+
values[i] = ((int) ((System.nanoTime() % 10000 * 0.0001) * Integer.MAX_VALUE));
186+
}
187+
188+
//sort generated data based on the instance varibale this.sorting
189+
if (this.sorting.equals(InputTypes.ASCENDING)) {
190+
values = IntQuickSort.quickSortAscend(values, 0, size - 1);
191+
}
192+
if (this.sorting.equals(InputTypes.DESCENDING)) {
193+
values = IntQuickSort.quickSortDescen(values, 0, size - 1);
194+
195+
}
196+
197+
return values;
198+
}
199+
175200
}

TreeTimeComplexity/src/main/java/utils/Benchmark.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package utils;
1818

19-
import algorithms.IntQuickSort;
2019
import algorithms.MinHeaps;
2120
import java.util.stream.LongStream;
2221

@@ -30,7 +29,6 @@ public class Benchmark {
3029
private MinHeaps heap;
3130
private int[] input;
3231
private int iterations;
33-
private InputTypes sorting;
3432

3533
/**
3634
* *
@@ -39,16 +37,12 @@ public class Benchmark {
3937
* @param heap Object to benchmark
4038
* @param iterations Number of iterations the benchmark will run. The
4139
* average running time calculation is based on the number of iterations.
42-
* @param inputLenght Lenght of randomised input data which will be used for
43-
* testing.
44-
* @param sorting Is the input data random or in ascending or descending
45-
* order.
40+
* @param testInput array of int to benchmark on
4641
*/
47-
public Benchmark(MinHeaps heap, int iterations, int inputLenght, InputTypes sorting) {
42+
public Benchmark(MinHeaps heap, int iterations, int[] testInput) {
4843
this.heap = heap;
4944
this.iterations = iterations;
50-
this.sorting = sorting;
51-
this.input = createTestData(inputLenght);
45+
this.input = testInput;
5246
}
5347

5448
// Public methods for benchmarking specific operations.
@@ -283,25 +277,4 @@ private long findMax(long[] array) {
283277
}
284278

285279
//Generate pseudorandom testdata from last digits of system.nanotime()
286-
private int[] createTestData(int size) {
287-
288-
int[] values = new int[size];
289-
290-
for (int i = 0; i < size; i++) {
291-
292-
values[i] = ((int) ((System.nanoTime() % 10000 * 0.0001) * Integer.MAX_VALUE));
293-
}
294-
295-
//sort generated data based on the instance varibale this.sorting
296-
if (this.sorting.equals(InputTypes.ASCENDING)) {
297-
values = IntQuickSort.quickSortAscend(values, 0, size - 1);
298-
}
299-
if (this.sorting.equals(InputTypes.DESCENDING)) {
300-
values = IntQuickSort.quickSortDescen(values, 0, size - 1);
301-
302-
}
303-
304-
return values;
305-
}
306-
307280
}

0 commit comments

Comments
 (0)