From 060ff596b80f45c3b19c7ee3b223f65729176744 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Wed, 11 Dec 2024 17:38:55 +0000 Subject: [PATCH] benchmark: SortEnum --- .../uni_marburg/powersort/benchmark/Main.java | 41 ++++--------------- .../powersort/benchmark/NaturalOrder.java | 4 +- .../powersort/data/AscendingIntegers.java | 8 ++-- .../powersort/data/DataArraySizes.java | 7 ++++ .../powersort/data/DescendingIntegers.java | 8 ++-- .../powersort/data/ObjectSupplier.java | 2 +- .../powersort/data/RandomIntegers.java | 8 ++-- .../powersort/sort/SimpleSort.java | 5 +++ .../uni_marburg/powersort/sort/SortEnum.java | 17 ++++++++ 9 files changed, 51 insertions(+), 49 deletions(-) create mode 100644 app/src/main/java/de/uni_marburg/powersort/data/DataArraySizes.java create mode 100644 app/src/main/java/de/uni_marburg/powersort/sort/SimpleSort.java create mode 100644 app/src/main/java/de/uni_marburg/powersort/sort/SortEnum.java 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 4c8a996..517e6f6 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,9 +1,7 @@ package de.uni_marburg.powersort.benchmark; import de.uni_marburg.powersort.data.DataEnum; -import de.uni_marburg.powersort.sort.DummySort; -import de.uni_marburg.powersort.sort.MergeSort; -import de.uni_marburg.powersort.sort.TimSort; +import de.uni_marburg.powersort.sort.SortEnum; import de.uni_marburg.powersort.data.ObjectSupplier; import java.util.EnumSet; @@ -14,55 +12,30 @@ import java.util.concurrent.TimeUnit; */ public class Main { public static void main(final String[] args) { - final SortImpl[] sortImplementations = getSortImplementations(); + final EnumSet sortImplementations = getSortImplementations(); final EnumSet dataEnums = getSortInputSuppliers(); for (DataEnum dataEnum : dataEnums) { ObjectSupplier objectSupplier = dataEnum.get(); System.out.println("\n" + objectSupplier.title()); - for (SortImpl sortImplementation : sortImplementations) { + for (SortEnum sortImplementation : sortImplementations) { Object[] sortInput = objectSupplier.getCopy(); final long startNanos = System.nanoTime(); - sortImplementation.sort(sortInput); + sortImplementation.get().sort(sortInput); final long stopNanos = System.nanoTime(); final long durNanos = stopNanos - startNanos; final long durMillis = TimeUnit.NANOSECONDS.toMillis(durNanos); final String durFormatted = LongFormatter.formatUnderscore(durMillis); - System.out.println(durFormatted + "," + sortImplementation.title); + System.out.println(durFormatted + "," + sortImplementation); } } } - static SortImpl[] getSortImplementations() { - return new SortImpl[]{ - new SortImpl("DummySort") { - @Override - public void sort(Object[] a) { - DummySort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0); - } - }, - new SortImpl("TimSort") { - @Override - public void sort(Object[] a) { - TimSort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0); - } - }, -// new SortImpl("FinnSort") { -// @Override -// public void sort(Object[] a) { -// FinnSort.sort(a); -// } -// }, - new SortImpl("MergeSort") { - @Override - public void sort(Object[] a) { - MergeSort.legacyMergeSort(a, NaturalOrder.INSTANCE); - } - } - }; + static EnumSet getSortImplementations() { + return EnumSet.allOf(SortEnum.class); } /** diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/NaturalOrder.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/NaturalOrder.java index e60074c..9fc8da8 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/NaturalOrder.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/NaturalOrder.java @@ -5,11 +5,11 @@ import java.util.Comparator; /** * Copied from JDK23 Arrays.java */ -final class NaturalOrder implements Comparator { +public final class NaturalOrder implements Comparator { @SuppressWarnings("unchecked") public int compare(Object first, Object second) { return ((Comparable) first).compareTo(second); } - static final NaturalOrder INSTANCE = new NaturalOrder(); + public static final NaturalOrder INSTANCE = new NaturalOrder(); } diff --git a/app/src/main/java/de/uni_marburg/powersort/data/AscendingIntegers.java b/app/src/main/java/de/uni_marburg/powersort/data/AscendingIntegers.java index 11e44b3..9a830c9 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/AscendingIntegers.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/AscendingIntegers.java @@ -3,15 +3,15 @@ package de.uni_marburg.powersort.data; import de.uni_marburg.powersort.benchmark.IntegerArray; import de.uni_marburg.powersort.benchmark.LongFormatter; -public class AscendingIntegers extends IntegerSupplier { - private static final int SIZE = 50_000_000; +import static de.uni_marburg.powersort.data.DataArraySizes.SIZE_ASC; +public class AscendingIntegers extends IntegerSupplier { public AscendingIntegers() { - super(IntegerArray.ascending(SIZE, 0)); + super(IntegerArray.ascending(SIZE_ASC, 1)); } @Override public String title() { - return "Array of " + LongFormatter.formatUnderscore(SIZE) + " ascending Integer objects."; + return "Array of " + LongFormatter.formatUnderscore(readOnly.length) + " ascending Integer objects."; } } diff --git a/app/src/main/java/de/uni_marburg/powersort/data/DataArraySizes.java b/app/src/main/java/de/uni_marburg/powersort/data/DataArraySizes.java new file mode 100644 index 0000000..627a63e --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/data/DataArraySizes.java @@ -0,0 +1,7 @@ +package de.uni_marburg.powersort.data; + +public interface DataArraySizes { + int SIZE_RAND = 50_000_000; + int SIZE_ASC = 50_000_000; + int SIZE_DESC = 50_000_000; +} diff --git a/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java b/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java index 4f3bea6..901b08b 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java @@ -3,15 +3,15 @@ package de.uni_marburg.powersort.data; import de.uni_marburg.powersort.benchmark.IntegerArray; import de.uni_marburg.powersort.benchmark.LongFormatter; -public class DescendingIntegers extends IntegerSupplier { - private static final int SIZE = 50_000_000; +import static de.uni_marburg.powersort.data.DataArraySizes.SIZE_DESC; +public class DescendingIntegers extends IntegerSupplier { public DescendingIntegers() { - super(IntegerArray.descending(SIZE, 0)); + super(IntegerArray.descending(SIZE_DESC, 1)); } @Override public String title() { - return "Array of " + LongFormatter.formatUnderscore(SIZE) + " descending Integer objects."; + return "Array of " + LongFormatter.formatUnderscore(readOnly.length) + " descending Integer objects."; } } 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 index c1decbc..e1bd1c8 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java @@ -3,7 +3,7 @@ package de.uni_marburg.powersort.data; import java.util.Arrays; public abstract class ObjectSupplier { - private final Object[] readOnly; + /* package-protected */ final Object[] readOnly; ObjectSupplier(Object[] readOnly) { this.readOnly = readOnly; diff --git a/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java b/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java index db29bf9..8cb36a9 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java @@ -3,15 +3,15 @@ package de.uni_marburg.powersort.data; import de.uni_marburg.powersort.benchmark.IntegerArray; import de.uni_marburg.powersort.benchmark.LongFormatter; -public class RandomIntegers extends IntegerSupplier { - private static final int SIZE = 50_000_000; +import static de.uni_marburg.powersort.data.DataArraySizes.SIZE_RAND; +public class RandomIntegers extends IntegerSupplier { public RandomIntegers() { - super(IntegerArray.random(SIZE)); + super(IntegerArray.random(SIZE_RAND)); } @Override public String title() { - return "Array of " + LongFormatter.formatUnderscore(SIZE) + " random Integer objects."; + return "Array of " + LongFormatter.formatUnderscore(readOnly.length) + " random Integer objects."; } } diff --git a/app/src/main/java/de/uni_marburg/powersort/sort/SimpleSort.java b/app/src/main/java/de/uni_marburg/powersort/sort/SimpleSort.java new file mode 100644 index 0000000..79e993e --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/sort/SimpleSort.java @@ -0,0 +1,5 @@ +package de.uni_marburg.powersort.sort; + +public interface SimpleSort { + void sort(Object[] list); +} diff --git a/app/src/main/java/de/uni_marburg/powersort/sort/SortEnum.java b/app/src/main/java/de/uni_marburg/powersort/sort/SortEnum.java new file mode 100644 index 0000000..151fb55 --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/sort/SortEnum.java @@ -0,0 +1,17 @@ +package de.uni_marburg.powersort.sort; + +import de.uni_marburg.powersort.benchmark.NaturalOrder; + +public enum SortEnum { +// BUBBLE_SORT, + MERGE_SORT, + TIM_SORT; + + public SimpleSort get() { + return switch (this) { +// case BUBBLE_SORT -> array -> BubbleSort.sort(array, NaturalOrder.INSTANCE); + case MERGE_SORT -> array -> MergeSort.legacyMergeSort(array, NaturalOrder.INSTANCE); + case TIM_SORT -> array -> TimSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0); + }; + } +}