benchmark: SortEnum

This commit is contained in:
Daniel Langbein 2024-12-11 17:38:55 +00:00
parent 904d462fc6
commit 060ff596b8
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
9 changed files with 51 additions and 49 deletions

View File

@ -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<SortEnum> sortImplementations = getSortImplementations();
final EnumSet<DataEnum> 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<SortEnum> getSortImplementations() {
return EnumSet.allOf(SortEnum.class);
}
/**

View File

@ -5,11 +5,11 @@ import java.util.Comparator;
/**
* Copied from JDK23 Arrays.java
*/
final class NaturalOrder implements Comparator<Object> {
public final class NaturalOrder implements Comparator<Object> {
@SuppressWarnings("unchecked")
public int compare(Object first, Object second) {
return ((Comparable<Object>) first).compareTo(second);
}
static final NaturalOrder INSTANCE = new NaturalOrder();
public static final NaturalOrder INSTANCE = new NaturalOrder();
}

View File

@ -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.";
}
}

View File

@ -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;
}

View File

@ -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.";
}
}

View File

@ -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;

View File

@ -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.";
}
}

View File

@ -0,0 +1,5 @@
package de.uni_marburg.powersort.sort;
public interface SimpleSort {
void sort(Object[] list);
}

View File

@ -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);
};
}
}