benchmark: refactor

This commit is contained in:
Daniel Langbein 2024-12-11 16:08:49 +00:00
parent e627c99b55
commit 684f28bb2b
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
4 changed files with 31 additions and 33 deletions

View File

@ -9,45 +9,43 @@ import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
// TODO: The parameters are way too low. Use for debugging only!
@Fork(1)
@Warmup(iterations = 3)
@Measurement(iterations = 6)
public class BenchmarkJmh {
/**
/*
* Benchmark parameters
*/
@Fork(0)
@Warmup(iterations = 0)
@Measurement(iterations = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
/*
* Benchmark state parameters
*
* Quote from JMH:
* State objects naturally encapsulate the state on which benchmark is working on.
*/
@State(Scope.Benchmark)
public static class State1 {
RandomIntegers d = new RandomIntegers();
Integer[] a;
public class BenchmarkJmh {
private final RandomIntegers readonly = new RandomIntegers();
/* package-protected */ Integer[] array;
// 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)
public void setup() {
a = d.get();
}
array = readonly.getCopy();
}
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Benchmark
public void rand1DummySort(State1 s) {
DummySort.sort(s.a, 0, s.a.length, NaturalOrder.INSTANCE, null, 0, 0);
public void rand1DummySort() {
DummySort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
}
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Benchmark
public void rand1TimSort(State1 s) {
TimSort.sort(s.a, 0, s.a.length, NaturalOrder.INSTANCE, null, 0, 0);
public void rand1TimSort() {
TimSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
}
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Benchmark
public void rand1MergeSort(State1 s) {
MergeSort.legacyMergeSort(s.a, NaturalOrder.INSTANCE);
public void rand1MergeSort() {
MergeSort.legacyMergeSort(array, NaturalOrder.INSTANCE);
}
}

View File

@ -25,7 +25,7 @@ public class Main {
System.out.println("\n" + objectSupplier.title());
for (SortImpl sortImplementation : sortImplementations) {
Object[] sortInput = objectSupplier.get();
Object[] sortInput = objectSupplier.getCopy();
final long startNanos = System.nanoTime();
sortImplementation.sort(sortInput);

View File

@ -6,7 +6,7 @@ public abstract class IntegerSupplier extends ObjectSupplier {
}
@Override
public Integer[] get() {
return (Integer[]) super.get();
public Integer[] getCopy() {
return (Integer[]) super.getCopy();
}
}

View File

@ -17,7 +17,7 @@ public abstract class ObjectSupplier {
/**
* @return A fresh copy of the array of objects represented by this object.
*/
public Object[] get(){
public Object[] getCopy(){
return Arrays.copyOf(readOnly, readOnly.length);
}
}