benchmark preparations

This commit is contained in:
Daniel Langbein 2024-12-09 11:26:57 +00:00
parent a84ec84903
commit 9d67b48215
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
4 changed files with 54 additions and 16 deletions

View File

@ -8,7 +8,7 @@ import java.util.Comparator;
* <p>
* Uses Arrays.sort() for sorting.
*/
class DummySort<T> {
class DummySort {
public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
T[] work, int workBase, int workLen) {
assert lo == 0;

View File

@ -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<T> {
class DummySort2 {
public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
T[] work, int workBase, int workLen) {
assert lo == 0;

View File

@ -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<Supplier<Object[]>> sortInputSuppliers = getSortInputSuppliers();
final NaturalOrder c = NaturalOrder.INSTANCE;
for (Supplier<Object[]> 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);
}
}
};
}

View File

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