diff --git a/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java b/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java index eeae33d..e096d7a 100644 --- a/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java +++ b/app/src/jmh/java/de/uni_marburg/powersort/benchmark/BenchmarkJmh.java @@ -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(); diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/IntegerArray.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/IntegerArray.java index dcc9069..911edc1 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/IntegerArray.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/IntegerArray.java @@ -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; + } } diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java index f8aa4f0..108251f 100644 --- a/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/Main.java @@ -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> 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 ); } } diff --git a/app/src/main/java/de/uni_marburg/powersort/data/Data1.java b/app/src/main/java/de/uni_marburg/powersort/data/Data1.java deleted file mode 100644 index 5fd8466..0000000 --- a/app/src/main/java/de/uni_marburg/powersort/data/Data1.java +++ /dev/null @@ -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); - } -} diff --git a/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java b/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java new file mode 100644 index 0000000..6a55b86 --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java @@ -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."; + } +} diff --git a/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java b/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java index f78ef76..3e0f392 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/IntegerSupplier.java @@ -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(); + } } diff --git a/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java b/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java index 400d635..6ddd7b7 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/ObjectSupplier.java @@ -1,8 +1,23 @@ package de.uni_marburg.powersort.data; -import java.util.function.Supplier; +import java.util.Arrays; -public interface ObjectSupplier extends Supplier { - 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); + } } diff --git a/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java b/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java new file mode 100644 index 0000000..3f1cf32 --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java @@ -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."; + } +}