mirror of
https://gitlab.uni-marburg.de/langbeid/powersort.git
synced 2025-01-22 19:55:44 +01:00
benchmark preparations
This commit is contained in:
parent
a84ec84903
commit
9d67b48215
@ -8,7 +8,7 @@ import java.util.Comparator;
|
|||||||
* <p>
|
* <p>
|
||||||
* Uses Arrays.sort() for sorting.
|
* 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,
|
public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
|
||||||
T[] work, int workBase, int workLen) {
|
T[] work, int workBase, int workLen) {
|
||||||
assert lo == 0;
|
assert lo == 0;
|
||||||
|
@ -9,7 +9,7 @@ import java.util.Comparator;
|
|||||||
* Uses Arrays.sort() for sorting.
|
* Uses Arrays.sort() for sorting.
|
||||||
* But calls it a.length times which makes this implementation really inefficient.
|
* 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,
|
public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
|
||||||
T[] work, int workBase, int workLen) {
|
T[] work, int workBase, int workLen) {
|
||||||
assert lo == 0;
|
assert lo == 0;
|
||||||
|
@ -2,34 +2,50 @@ package de.uni_marburg.powersort.benchmark;
|
|||||||
|
|
||||||
import de.uni_marburg.powersort.TimSort;
|
import de.uni_marburg.powersort.TimSort;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(final String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
|
public static void main(final String[] args) {
|
||||||
final Method[] sortImplementations = getSortImplementations();
|
final SortImpl[] sortImplementations = getSortImplementations();
|
||||||
final List<Supplier<Object[]>> sortInputSuppliers = getSortInputSuppliers();
|
final List<Supplier<Object[]>> sortInputSuppliers = getSortInputSuppliers();
|
||||||
|
|
||||||
final NaturalOrder c = NaturalOrder.INSTANCE;
|
|
||||||
for (Supplier<Object[]> sortInputSupplier : sortInputSuppliers) {
|
for (Supplier<Object[]> sortInputSupplier : sortInputSuppliers) {
|
||||||
for (Method sortImplementation : sortImplementations) {
|
for (SortImpl sortImplementation : sortImplementations) {
|
||||||
Object[] sortInput = sortInputSupplier.get();
|
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 {
|
static SortImpl[] getSortImplementations() {
|
||||||
return new Method[]{
|
return new SortImpl[]{
|
||||||
DummySort.class.getMethod("sort", Object[].class, int.class, int.class, Comparator.class, Object[].class, int.class, int.class),
|
new SortImpl("DummySort", "DummySort.sort(a,c) uses Arrays.sort(a,c) internally.") {
|
||||||
DummySort2.class.getMethod("sort", Object[].class, int.class, int.class, Comparator.class, Object[].class, int.class, int.class),
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user