benchmark preparations

This commit is contained in:
Daniel Langbein 2024-12-09 14:00:12 +00:00
parent 3ad2afb5f2
commit 53bd2c410e
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
6 changed files with 62 additions and 27 deletions

View File

@ -1,6 +1,7 @@
package de.uni_marburg.powersort.benchmark;
import de.uni_marburg.powersort.TimSort;
import de.uni_marburg.powersort.data.Data1;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
@ -14,12 +15,12 @@ import java.util.concurrent.TimeUnit;
public class BenchmarkJmh {
@State(Scope.Benchmark)
public static class State1 {
Integer[] readOnly = IntegerArray.random(500_000);;
Data1 d = new Data1();
Integer[] a;
@Setup(Level.Invocation)
public void setup() {
a = Arrays.copyOf(readOnly, readOnly.length);
a = d.get();
}
}

View File

@ -1,6 +1,8 @@
package de.uni_marburg.powersort.benchmark;
import de.uni_marburg.powersort.TimSort;
import de.uni_marburg.powersort.data.Data1;
import de.uni_marburg.powersort.data.ObjectSupplier;
import java.util.Arrays;
import java.util.List;
@ -12,50 +14,54 @@ import java.util.function.Supplier;
public class Main {
public static void main(final String[] args) {
final SortImpl[] sortImplementations = getSortImplementations();
final List<Supplier<Object[]>> sortInputSuppliers = getSortInputSuppliers();
final List<Supplier<ObjectSupplier>> sortInputSuppliers = getSortInputSuppliers();
for (Supplier<ObjectSupplier> sortInputSupplier : sortInputSuppliers) {
ObjectSupplier objectSupplier = sortInputSupplier.get();
System.out.println("\n" + objectSupplier.title());
for (Supplier<Object[]> sortInputSupplier : sortInputSuppliers) {
for (SortImpl sortImplementation : sortImplementations) {
Object[] sortInput = sortInputSupplier.get();
Object[] sortInput = objectSupplier.get();
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);
System.out.println(duration + "," + sortImplementation.title);
}
}
}
static SortImpl[] getSortImplementations() {
return new SortImpl[]{
new SortImpl("DummySort", "DummySort.sort(a,c) uses Arrays.sort(a,c) internally.") {
new SortImpl("DummySort") {
@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);
}
},
//
new SortImpl("TimSort", "Copy of JDK23 TimSort.java") {
new SortImpl("TimSort") {
@Override
public void sort(Object[] a) {
TimSort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0);
}
},
new SortImpl("MergeSort") {
@Override
public void sort(Object[] a) {
MergeSort.legacyMergeSort(a, NaturalOrder.INSTANCE);
}
}
};
}
static List<Supplier<Object[]>> getSortInputSuppliers() {
static List<Supplier<ObjectSupplier>> getSortInputSuppliers() {
return Arrays.asList(
() -> new DummyComparable2[]{new DummyComparable2(0), new DummyComparable2(1)},
() -> new DummyComparable2[]{new DummyComparable2(2), new DummyComparable2(3)}
// Three different random lists.
Data1::new,
Data1::new,
Data1::new
);
}
}

View File

@ -1,18 +1,10 @@
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) {
public SortImpl(String title) {
this.title = title;
this.description = description;
}
/**

View File

@ -0,0 +1,20 @@
package de.uni_marburg.powersort.data;
import de.uni_marburg.powersort.benchmark.IntegerArray;
import java.util.Arrays;
public class Data1 implements IntegerSupplier {
private static final int SIZE = 500_000;
private final Integer[] readOnly = IntegerArray.random(SIZE);
@Override
public String title() {
return "Array of random Integers of size " + SIZE;
}
@Override
public Integer[] get() {
return Arrays.copyOf(readOnly, readOnly.length);
}
}

View File

@ -0,0 +1,8 @@
package de.uni_marburg.powersort.data;
import java.util.function.Supplier;
public interface IntegerSupplier extends ObjectSupplier {
String title();
Integer[] get();
}

View File

@ -0,0 +1,8 @@
package de.uni_marburg.powersort.data;
import java.util.function.Supplier;
public interface ObjectSupplier extends Supplier<Object[]> {
String title();
Object[] get();
}