diff --git a/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java b/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java index c114f4b..eeae33d 100644 --- a/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java +++ b/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java @@ -1,6 +1,7 @@ package de.uni_marburg.powersort.benchmark; import de.uni_marburg.powersort.TimSort; +import de.uni_marburg.powersort.data.Data1; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.infra.Blackhole; @@ -14,12 +15,12 @@ import java.util.concurrent.TimeUnit; public class BenchmarkJmh { @State(Scope.Benchmark) public static class State1 { - Integer[] readOnly = IntegerArray.random(500_000);; + Data1 d = new Data1(); Integer[] a; @Setup(Level.Invocation) public void setup() { - a = Arrays.copyOf(readOnly, readOnly.length); + a = d.get(); } } diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java index adadf7c..f8aa4f0 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java @@ -1,6 +1,8 @@ package de.uni_marburg.powersort.benchmark; import de.uni_marburg.powersort.TimSort; +import de.uni_marburg.powersort.data.Data1; +import de.uni_marburg.powersort.data.ObjectSupplier; import java.util.Arrays; import java.util.List; @@ -12,50 +14,54 @@ import java.util.function.Supplier; public class Main { public static void main(final String[] args) { final SortImpl[] sortImplementations = getSortImplementations(); - final List> sortInputSuppliers = getSortInputSuppliers(); + final List> sortInputSuppliers = getSortInputSuppliers(); + + for (Supplier sortInputSupplier : sortInputSuppliers) { + ObjectSupplier objectSupplier = sortInputSupplier.get(); + System.out.println("\n" + objectSupplier.title()); - for (Supplier sortInputSupplier : sortInputSuppliers) { for (SortImpl sortImplementation : sortImplementations) { - Object[] sortInput = sortInputSupplier.get(); + Object[] sortInput = objectSupplier.get(); final long startTime = System.nanoTime(); sortImplementation.sort(sortInput); final long stopTime = System.nanoTime(); final long duration = stopTime - startTime; - System.out.println(duration + "," + sortImplementation.title + "," + sortImplementation.description); + System.out.println(duration + "," + sortImplementation.title); } } } static SortImpl[] getSortImplementations() { return new SortImpl[]{ - new SortImpl("DummySort", "DummySort.sort(a,c) uses Arrays.sort(a,c) internally.") { + new SortImpl("DummySort") { @Override public void sort(Object[] a) { DummySort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0); } }, - new SortImpl("DummySort2", "DummySort2.sort(a,c) uses Arrays.sort(a,c) internally. It calles that method multiple times making this implementation really slow.") { - @Override - public void sort(Object[] a) { - DummySort2.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0); - } - }, - // - new SortImpl("TimSort", "Copy of JDK23 TimSort.java") { + new SortImpl("TimSort") { @Override public void sort(Object[] a) { TimSort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0); } + }, + new SortImpl("MergeSort") { + @Override + public void sort(Object[] a) { + MergeSort.legacyMergeSort(a, NaturalOrder.INSTANCE); + } } }; } - static List> getSortInputSuppliers() { + static List> getSortInputSuppliers() { return Arrays.asList( - () -> new DummyComparable2[]{new DummyComparable2(0), new DummyComparable2(1)}, - () -> new DummyComparable2[]{new DummyComparable2(2), new DummyComparable2(3)} + // Three different random lists. + Data1::new, + Data1::new, + Data1::new ); } } diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/SortImpl.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/SortImpl.java index 71698fe..e9552fd 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/SortImpl.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/SortImpl.java @@ -1,18 +1,10 @@ package de.uni_marburg.powersort.benchmark; public abstract class SortImpl { - /** - * Title of the sort algorithm used. - */ final String title; - /** - * Implementation details of the sort algorithm used. - */ - final String description; - public SortImpl(String title, String description) { + public SortImpl(String title) { this.title = title; - this.description = description; } /** diff --git a/app/src/main/java/de/uni_marburg/powersort/data/Data1.java b/app/src/main/java/de/uni_marburg/powersort/data/Data1.java new file mode 100644 index 0000000..5fd8466 --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/data/Data1.java @@ -0,0 +1,20 @@ +package de.uni_marburg.powersort.data; + +import de.uni_marburg.powersort.benchmark.IntegerArray; + +import java.util.Arrays; + +public class Data1 implements IntegerSupplier { + private static final int SIZE = 500_000; + private final Integer[] readOnly = IntegerArray.random(SIZE); + + @Override + public String title() { + return "Array of random Integers of size " + SIZE; + } + + @Override + public Integer[] get() { + return Arrays.copyOf(readOnly, readOnly.length); + } +} diff --git a/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java b/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java new file mode 100644 index 0000000..f78ef76 --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java @@ -0,0 +1,8 @@ +package de.uni_marburg.powersort.data; + +import java.util.function.Supplier; + +public interface IntegerSupplier extends ObjectSupplier { + String title(); + Integer[] get(); +} diff --git a/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java b/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java new file mode 100644 index 0000000..400d635 --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java @@ -0,0 +1,8 @@ +package de.uni_marburg.powersort.data; + +import java.util.function.Supplier; + +public interface ObjectSupplier extends Supplier { + String title(); + Object[] get(); +}