diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/DummySort.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/DummySort.java index 16e4c76..ba9e4a5 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/DummySort.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/DummySort.java @@ -8,7 +8,7 @@ import java.util.Comparator; *

* Uses Arrays.sort() for sorting. */ -class DummySort { +class DummySort { public static void sort(T[] a, int lo, int hi, Comparator c, T[] work, int workBase, int workLen) { assert lo == 0; diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/DummySort2.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/DummySort2.java index 96edd2d..e44b3f7 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/DummySort2.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/DummySort2.java @@ -9,7 +9,7 @@ import java.util.Comparator; * Uses Arrays.sort() for sorting. * But calls it a.length times which makes this implementation really inefficient. */ -class DummySort2 { +class DummySort2 { public static void sort(T[] a, int lo, int hi, Comparator c, T[] work, int workBase, int workLen) { assert lo == 0; 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 da019e3..fe0e492 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 @@ -2,34 +2,50 @@ package de.uni_marburg.powersort.benchmark; import de.uni_marburg.powersort.TimSort; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.Arrays; -import java.util.Comparator; import java.util.List; import java.util.function.Supplier; public class Main { - public static void main(final String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { - final Method[] sortImplementations = getSortImplementations(); + public static void main(final String[] args) { + final SortImpl[] sortImplementations = getSortImplementations(); final List> sortInputSuppliers = getSortInputSuppliers(); - final NaturalOrder c = NaturalOrder.INSTANCE; for (Supplier sortInputSupplier : sortInputSuppliers) { - for (Method sortImplementation : sortImplementations) { + for (SortImpl sortImplementation : sortImplementations) { Object[] sortInput = sortInputSupplier.get(); - sortImplementation.invoke(null, sortInput, 0, 0, c, null, 0, 0); - System.out.println(Arrays.toString(sortInput)); + + 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); } } } - static Method[] getSortImplementations() throws NoSuchMethodException { - return new Method[]{ - DummySort.class.getMethod("sort", Object[].class, int.class, int.class, Comparator.class, Object[].class, int.class, int.class), - DummySort2.class.getMethod("sort", Object[].class, int.class, int.class, Comparator.class, Object[].class, int.class, int.class), + static SortImpl[] getSortImplementations() { + return new SortImpl[]{ + new SortImpl("DummySort", "DummySort.sort(a,c) uses Arrays.sort(a,c) internally.") { + @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); + } + }, // - TimSort.class.getMethod("sort", Object[].class, int.class, int.class, Comparator.class, Object[].class, int.class, int.class), + new SortImpl("TimSort", "Copy of JDK23 TimSort.java") { + @Override + public void sort(Object[] a) { + TimSort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0); + } + } }; } 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 new file mode 100644 index 0000000..71698fe --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/SortImpl.java @@ -0,0 +1,22 @@ +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) { + this.title = title; + this.description = description; + } + + /** + * Apply the sort algorithm. + */ + public abstract void sort(Object[] a); +}