jdk sort intro & benchmark preparations

This commit is contained in:
Daniel Langbein 2024-12-08 12:10:04 +00:00
parent 9f8ca86ac0
commit 0b5c6df766
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
6 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package de.uni_marburg.powersort.benchmark;
import java.util.Arrays;
import java.util.Comparator;
/**
* Same class and method interface as JDK23 TimSort.java with its sort() method.
* <p>
* Uses Arrays.sort() for sorting.
*/
class DummySort<T> {
public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
T[] work, int workBase, int workLen) {
assert lo == 0;
assert hi == a.length;
assert work == null;
Arrays.sort(a, c);
}
}

View File

@ -0,0 +1,23 @@
package de.uni_marburg.powersort.benchmark;
import java.util.Arrays;
import java.util.Comparator;
/**
* Same class and method interface as TimSort.java with its sort() method.
* <p>
* Uses Arrays.sort() for sorting.
* But calls it a.length times which makes this implementation really inefficient.
*/
class DummySort2<T> {
public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
T[] work, int workBase, int workLen) {
assert lo == 0;
assert hi == a.length;
assert work == null;
for (int i = 0; i < a.length; i++) {
Arrays.sort(a, c);
}
}
}

View File

@ -0,0 +1,62 @@
package de.uni_marburg.powersort.benchmark;
import java.util.Arrays;
/**
* Quick intro on how sorting arrays works in JDK 23.
*/
public class Educational {
public static void main(String[] args) {
sortObjectsAvoidingComparableTimSort();
sortObjects();
sortPrimitives();
}
/**
* TODO: If this is no slower than sortObjects(), then we could avoid ComparableTimSort :)
* <p>
* Quote from JDK23 ComparableTimSort.java
* <p>
* If you are using an optimizing VM, you may find that ComparableTimSort
* offers no performance benefit over TimSort in conjunction with a
* comparator that simply returns {@code ((Comparable)first).compareTo(Second)}.
* If this is the case, you are better off deleting ComparableTimSort to
* eliminate the code duplication.
*/
private static void sortObjectsAvoidingComparableTimSort() {
SomeComparable[] a = {
new SomeComparable(1),
new SomeComparable(0)
};
NaturalOrder c = NaturalOrder.INSTANCE;
// -> TimSort.sort(a, 0, a.length, c, null, 0, 0);
// -> All elements in the array must implement the {@link Comparable} interface.
Arrays.sort(a, c);
System.out.println(Arrays.toString(a));
}
public static void sortObjects() {
SomeComparable[] a = {
new SomeComparable(1),
new SomeComparable(0)
};
// -> ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
// -> All elements in the array must implement the {@link Comparable} interface.
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
public static void sortPrimitives() {
int[] a = {1, 0};
// -> DualPivotQuicksort.sort(a, 0, 0, a.length);
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}

View File

@ -0,0 +1,30 @@
package de.uni_marburg.powersort.benchmark;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(final String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
final SomeComparable[] a = {
new SomeComparable(1),
new SomeComparable(0)
};
final Method[] sortImplementations = getSortImplementations();
final NaturalOrder c = NaturalOrder.INSTANCE;
for (Method sortImplementation : sortImplementations) {
sortImplementation.invoke(null, a, 0, 0, c, null, 0, 0);
System.out.println(Arrays.toString(a));
}
}
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)
};
}
}

View File

@ -0,0 +1,15 @@
package de.uni_marburg.powersort.benchmark;
import java.util.Comparator;
/**
* Copied from JDK23 Arrays.java
*/
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();
}

View File

@ -0,0 +1,11 @@
package de.uni_marburg.powersort.benchmark;
/**
* A class for tiny, comparable objects.
*/
record SomeComparable(int id) implements Comparable<SomeComparable> {
@Override
public int compareTo(SomeComparable other) {
return id - other.id;
}
}