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

View File

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

View File

@ -6,7 +6,7 @@ public abstract class IntegerSupplier extends ObjectSupplier {
} }
@Override @Override
public Integer[] get() { public Integer[] getCopy() {
return (Integer[]) super.get(); 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. * @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); return Arrays.copyOf(readOnly, readOnly.length);
} }
} }