benchmark preparations

This commit is contained in:
Daniel Langbein 2024-12-09 14:46:52 +00:00
parent 53bd2c410e
commit 1194e6cbbe
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
8 changed files with 92 additions and 35 deletions

View File

@ -1,11 +1,10 @@
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.RandomIntegers;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
// TODO: The parameters are way too low. Use for debugging only!
@ -15,9 +14,11 @@ import java.util.concurrent.TimeUnit;
public class BenchmarkJmh {
@State(Scope.Benchmark)
public static class State1 {
Data1 d = new Data1();
RandomIntegers d = new RandomIntegers();
Integer[] a;
// TODO: This is inaccurate.
// How to create and use separate arrays for each warmup x iteration x sortAlgorithm ?
@Setup(Level.Invocation)
public void setup() {
a = d.get();

View File

@ -15,4 +15,21 @@ public class IntegerArray {
}
return list;
}
/**
* @return [start, start-1, ..., end+1, end]
*/
public static Integer[] descending(final int start, final int end) {
assert start > end;
Integer[] list = new Integer[start - end + 1];
for (int i = 0; i < list.length; i++) {
int value = start - i;
list[i] = value;
}
assert list[0] == start;
assert list[list.length - 1] == end;
return list;
}
}

View File

@ -1,7 +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.DescendingIntegers;
import de.uni_marburg.powersort.data.RandomIntegers;
import de.uni_marburg.powersort.data.ObjectSupplier;
import java.util.Arrays;
@ -56,12 +57,19 @@ public class Main {
};
}
/**
* The returned ObjectSupplier objects are wrapped by Supplier objects.
* This way they are lazily created on their first access.
* This saves memory if we work with large lists.
*/
static List<Supplier<ObjectSupplier>> getSortInputSuppliers() {
return Arrays.asList(
// Three different random lists.
Data1::new,
Data1::new,
Data1::new
RandomIntegers::new,
RandomIntegers::new,
RandomIntegers::new,
// One descending list.
DescendingIntegers::new
);
}
}

View File

@ -1,20 +0,0 @@
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,16 @@
package de.uni_marburg.powersort.data;
import de.uni_marburg.powersort.benchmark.IntegerArray;
public class DescendingIntegers extends IntegerSupplier {
private static final int SIZE = 50_000_000;
public DescendingIntegers() {
super(IntegerArray.descending(SIZE, 0));
}
@Override
public String title() {
return "Array of " + SIZE + " descending Integer objects.";
}
}

View File

@ -1,8 +1,12 @@
package de.uni_marburg.powersort.data;
import java.util.function.Supplier;
public abstract class IntegerSupplier extends ObjectSupplier {
IntegerSupplier(Object[] readOnly) {
super(readOnly);
}
public interface IntegerSupplier extends ObjectSupplier {
String title();
Integer[] get();
@Override
public Integer[] get() {
return (Integer[]) super.get();
}
}

View File

@ -1,8 +1,23 @@
package de.uni_marburg.powersort.data;
import java.util.function.Supplier;
import java.util.Arrays;
public interface ObjectSupplier extends Supplier<Object[]> {
String title();
Object[] get();
public abstract class ObjectSupplier {
final Object[] readOnly;
ObjectSupplier(Object[] readOnly) {
this.readOnly = readOnly;
}
/**
* Descriptive title for the array of objects represented by this object.
*/
public abstract String title();
/**
* @return A fresh copy of the array of objects represented by this object.
*/
public Object[] get(){
return Arrays.copyOf(readOnly, readOnly.length);
}
}

View File

@ -0,0 +1,16 @@
package de.uni_marburg.powersort.data;
import de.uni_marburg.powersort.benchmark.IntegerArray;
public class RandomIntegers extends IntegerSupplier {
private static final int SIZE = 50_000_000;
public RandomIntegers() {
super(IntegerArray.random(SIZE));
}
@Override
public String title() {
return "Array of " + SIZE + " random Integer objects.";
}
}