mirror of
https://gitlab.uni-marburg.de/langbeid/powersort.git
synced 2025-01-22 19:55:44 +01:00
benchmark: @Param & DataEnum
This commit is contained in:
parent
605d0c2781
commit
904d462fc6
@ -1,7 +1,19 @@
|
|||||||
package de.uni_marburg.powersort.benchmark;
|
package de.uni_marburg.powersort.benchmark;
|
||||||
|
|
||||||
import de.uni_marburg.powersort.data.RandomIntegers;
|
import de.uni_marburg.powersort.data.DataEnum;
|
||||||
import org.openjdk.jmh.annotations.*;
|
import de.uni_marburg.powersort.data.ObjectSupplier;
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
|
import org.openjdk.jmh.annotations.Level;
|
||||||
|
import org.openjdk.jmh.annotations.Measurement;
|
||||||
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
|
import org.openjdk.jmh.annotations.Param;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.Setup;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
import org.openjdk.jmh.annotations.Warmup;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@ -22,13 +34,23 @@ import java.util.concurrent.TimeUnit;
|
|||||||
*/
|
*/
|
||||||
@State(Scope.Benchmark)
|
@State(Scope.Benchmark)
|
||||||
public abstract class AbstractBenchmark {
|
public abstract class AbstractBenchmark {
|
||||||
private final RandomIntegers readonly = new RandomIntegers();
|
@Param()
|
||||||
/* package-protected */ Integer[] array;
|
private DataEnum dataEnum;
|
||||||
|
|
||||||
|
private DataEnum prevDataEnum = null;
|
||||||
|
private ObjectSupplier data;
|
||||||
|
|
||||||
|
/* package-protected */ Object[] workingCopy;
|
||||||
|
|
||||||
// TODO: This is inaccurate. How to create and use separate arrays for each warmup x iteration x sortAlgorithm ?
|
// TODO: This is inaccurate. How to create and use separate arrays for each warmup x iteration x sortAlgorithm ?
|
||||||
@Setup(Level.Invocation)
|
@Setup(Level.Invocation)
|
||||||
public void setup() {
|
public void setup() {
|
||||||
array = readonly.getCopy();
|
if (dataEnum != prevDataEnum){
|
||||||
|
System.out.println(prevDataEnum + " => " + dataEnum);
|
||||||
|
prevDataEnum = dataEnum;
|
||||||
|
data = dataEnum.get();
|
||||||
|
}
|
||||||
|
workingCopy = data.getCopy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
|
@ -5,6 +5,6 @@ import de.uni_marburg.powersort.sort.DummySort;
|
|||||||
public class DummySortBenchmark extends AbstractBenchmark {
|
public class DummySortBenchmark extends AbstractBenchmark {
|
||||||
@Override
|
@Override
|
||||||
public void sort() {
|
public void sort() {
|
||||||
DummySort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
|
DummySort.sort(workingCopy, 0, workingCopy.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,6 @@ import de.uni_marburg.powersort.sort.MergeSort;
|
|||||||
public class MergeSortBenchmark extends AbstractBenchmark {
|
public class MergeSortBenchmark extends AbstractBenchmark {
|
||||||
@Override
|
@Override
|
||||||
public void sort() {
|
public void sort() {
|
||||||
MergeSort.legacyMergeSort(array, NaturalOrder.INSTANCE);
|
MergeSort.legacyMergeSort(workingCopy, NaturalOrder.INSTANCE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,19 +17,35 @@ public class IntegerArray {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return [start, start-1, ..., end+1, end]
|
* @return [high, high-1, ..., low+1, low]
|
||||||
*/
|
*/
|
||||||
public static Integer[] descending(final int start, final int end) {
|
public static Integer[] descending(final int high, final int low) {
|
||||||
assert start > end;
|
assert high > low;
|
||||||
|
|
||||||
Integer[] list = new Integer[start - end + 1];
|
Integer[] list = new Integer[high - low + 1];
|
||||||
for (int i = 0; i < list.length; i++) {
|
for (int i = 0; i < list.length; i++) {
|
||||||
int value = start - i;
|
int value = high - i;
|
||||||
list[i] = value;
|
list[i] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert list[0] == start;
|
assert list[0] == high;
|
||||||
assert list[list.length - 1] == end;
|
assert list[list.length - 1] == low;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return [low, low-1, ..., high+1, high]
|
||||||
|
*/
|
||||||
|
public static Integer[] ascending(final int low, final int high) {
|
||||||
|
assert low < high;
|
||||||
|
|
||||||
|
Integer[] list = new Integer[low - high + 1];
|
||||||
|
for (int i = 0; i < list.length; i++) {
|
||||||
|
int value = low + i;
|
||||||
|
list[i] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert list[0] == low;
|
||||||
|
assert list[list.length - 1] == high;
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
package de.uni_marburg.powersort.benchmark;
|
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.DummySort;
|
||||||
import de.uni_marburg.powersort.sort.MergeSort;
|
import de.uni_marburg.powersort.sort.MergeSort;
|
||||||
import de.uni_marburg.powersort.sort.TimSort;
|
import de.uni_marburg.powersort.sort.TimSort;
|
||||||
import de.uni_marburg.powersort.data.DescendingIntegers;
|
|
||||||
import de.uni_marburg.powersort.data.RandomIntegers;
|
|
||||||
import de.uni_marburg.powersort.data.ObjectSupplier;
|
import de.uni_marburg.powersort.data.ObjectSupplier;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom benchmark.
|
* Custom benchmark.
|
||||||
@ -18,10 +15,10 @@ import java.util.function.Supplier;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
final SortImpl[] sortImplementations = getSortImplementations();
|
final SortImpl[] sortImplementations = getSortImplementations();
|
||||||
final List<Supplier<ObjectSupplier>> sortInputSuppliers = getSortInputSuppliers();
|
final EnumSet<DataEnum> dataEnums = getSortInputSuppliers();
|
||||||
|
|
||||||
for (Supplier<ObjectSupplier> sortInputSupplier : sortInputSuppliers) {
|
for (DataEnum dataEnum : dataEnums) {
|
||||||
ObjectSupplier objectSupplier = sortInputSupplier.get();
|
ObjectSupplier objectSupplier = dataEnum.get();
|
||||||
System.out.println("\n" + objectSupplier.title());
|
System.out.println("\n" + objectSupplier.title());
|
||||||
|
|
||||||
for (SortImpl sortImplementation : sortImplementations) {
|
for (SortImpl sortImplementation : sortImplementations) {
|
||||||
@ -53,6 +50,12 @@ public class Main {
|
|||||||
TimSort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0);
|
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") {
|
new SortImpl("MergeSort") {
|
||||||
@Override
|
@Override
|
||||||
public void sort(Object[] a) {
|
public void sort(Object[] a) {
|
||||||
@ -63,18 +66,11 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The returned ObjectSupplier objects are wrapped by Supplier objects.
|
* The returned ObjectSupplier objects are wrapped by DataEnum objects.
|
||||||
* This way they are lazily created on their first access.
|
* This way they are lazily created on their first access with DataEnum.get().
|
||||||
* This saves memory if we work with large lists.
|
* This saves memory if we work with large lists.
|
||||||
*/
|
*/
|
||||||
static List<Supplier<ObjectSupplier>> getSortInputSuppliers() {
|
static EnumSet<DataEnum> getSortInputSuppliers() {
|
||||||
return Arrays.asList(
|
return EnumSet.allOf(DataEnum.class);
|
||||||
// Three different random lists.
|
|
||||||
RandomIntegers::new,
|
|
||||||
RandomIntegers::new,
|
|
||||||
RandomIntegers::new,
|
|
||||||
// One descending list.
|
|
||||||
DescendingIntegers::new
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
public AscendingIntegers() {
|
||||||
|
super(IntegerArray.ascending(SIZE, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String title() {
|
||||||
|
return "Array of " + LongFormatter.formatUnderscore(SIZE) + " ascending Integer objects.";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package de.uni_marburg.powersort.data;
|
||||||
|
|
||||||
|
public enum DataEnum {
|
||||||
|
RANDOM_INTEGERS,
|
||||||
|
ASCENDING_INTEGERS,
|
||||||
|
DESCENDING_INTEGERS;
|
||||||
|
|
||||||
|
public ObjectSupplier get() {
|
||||||
|
return switch (this) {
|
||||||
|
case RANDOM_INTEGERS -> new RandomIntegers();
|
||||||
|
case ASCENDING_INTEGERS -> new AscendingIntegers();
|
||||||
|
case DESCENDING_INTEGERS -> new DescendingIntegers();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ package de.uni_marburg.powersort.data;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public abstract class ObjectSupplier {
|
public abstract class ObjectSupplier {
|
||||||
final Object[] readOnly;
|
private final Object[] readOnly;
|
||||||
|
|
||||||
ObjectSupplier(Object[] readOnly) {
|
ObjectSupplier(Object[] readOnly) {
|
||||||
this.readOnly = readOnly;
|
this.readOnly = readOnly;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user