From 8e20550b44c8b98868d0a49c22e1386c4d67d9f5 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Tue, 10 Dec 2024 11:00:11 +0000 Subject: [PATCH] benchmark preparations --- .../powersort/benchmark/BenchmarkJmh.java | 6 +- .../powersort/benchmark/LongFormatter.java | 36 ++++++++++++ .../uni_marburg/powersort/benchmark/Main.java | 11 ++-- .../powersort/data/DescendingIntegers.java | 3 +- .../powersort/data/RandomIntegers.java | 3 +- .../benchmark/LongFormatterTest.java | 57 +++++++++++++++++++ 6 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/de/uni_marburg/powersort/benchmark/LongFormatter.java create mode 100644 app/src/test/java/de/uni_marburg/powersort/benchmark/LongFormatterTest.java 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 e096d7a..197b496 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 @@ -8,9 +8,9 @@ import org.openjdk.jmh.infra.Blackhole; import java.util.concurrent.TimeUnit; // TODO: The parameters are way too low. Use for debugging only! -@Fork(0) -@Warmup(iterations = 0) -@Measurement(iterations = 3) +@Fork(1) +@Warmup(iterations = 3) +@Measurement(iterations = 6) public class BenchmarkJmh { @State(Scope.Benchmark) public static class State1 { diff --git a/app/src/main/java/de/uni_marburg/powersort/benchmark/LongFormatter.java b/app/src/main/java/de/uni_marburg/powersort/benchmark/LongFormatter.java new file mode 100644 index 0000000..154021b --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/benchmark/LongFormatter.java @@ -0,0 +1,36 @@ +package de.uni_marburg.powersort.benchmark; + +public class LongFormatter { + /** + * @param l Number to format. + * @return Number as formatted String. Thousands are separated by underscore `_`. + */ + public static String formatUnderscore(long l) { + int firstDigitIdx = 0; + // If l is negative, its string starts with '-'. + // Thus, the first digit is at idx 1. + if (l < 0) { + firstDigitIdx = 1; + } + + StringBuilder durFormatted = new StringBuilder(); + char[] digits = String.valueOf(l).toCharArray(); + int remainder = digits.length % 3; + for (int i = 0; i < digits.length; i++) { + boolean firstOrLastDigit = i <= firstDigitIdx || i == digits.length - 1; + if (!firstOrLastDigit && i % 3 == remainder) { + durFormatted.append('_'); + } + durFormatted.append(digits[i]); + } + return durFormatted.toString(); + } + + /** + * @param i Number to format. + * @return Number as formatted String. Thousands are separated by underscore `_`. + */ + public static String formatUnderscore(int i) { + return formatUnderscore((long) i); + } +} 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 108251f..39c3938 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 @@ -7,6 +7,7 @@ import de.uni_marburg.powersort.data.ObjectSupplier; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.function.Supplier; /** @@ -24,12 +25,14 @@ public class Main { for (SortImpl sortImplementation : sortImplementations) { Object[] sortInput = objectSupplier.get(); - final long startTime = System.nanoTime(); + final long startNanos = System.nanoTime(); sortImplementation.sort(sortInput); - final long stopTime = System.nanoTime(); - final long duration = stopTime - startTime; + final long stopNanos = System.nanoTime(); - System.out.println(duration + "," + sortImplementation.title); + final long durNanos = stopNanos - startNanos; + final long durMillis = TimeUnit.NANOSECONDS.toMillis(durNanos); + final String durFormatted = LongFormatter.formatUnderscore(durMillis); + System.out.println(durFormatted + "," + sortImplementation.title); } } } 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 index 6a55b86..4f3bea6 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/DescendingIntegers.java @@ -1,6 +1,7 @@ package de.uni_marburg.powersort.data; import de.uni_marburg.powersort.benchmark.IntegerArray; +import de.uni_marburg.powersort.benchmark.LongFormatter; public class DescendingIntegers extends IntegerSupplier { private static final int SIZE = 50_000_000; @@ -11,6 +12,6 @@ public class DescendingIntegers extends IntegerSupplier { @Override public String title() { - return "Array of " + SIZE + " descending Integer objects."; + return "Array of " + LongFormatter.formatUnderscore(SIZE) + " descending Integer objects."; } } 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 index 3f1cf32..db29bf9 100644 --- a/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java +++ b/app/src/main/java/de/uni_marburg/powersort/data/RandomIntegers.java @@ -1,6 +1,7 @@ package de.uni_marburg.powersort.data; import de.uni_marburg.powersort.benchmark.IntegerArray; +import de.uni_marburg.powersort.benchmark.LongFormatter; public class RandomIntegers extends IntegerSupplier { private static final int SIZE = 50_000_000; @@ -11,6 +12,6 @@ public class RandomIntegers extends IntegerSupplier { @Override public String title() { - return "Array of " + SIZE + " random Integer objects."; + return "Array of " + LongFormatter.formatUnderscore(SIZE) + " random Integer objects."; } } diff --git a/app/src/test/java/de/uni_marburg/powersort/benchmark/LongFormatterTest.java b/app/src/test/java/de/uni_marburg/powersort/benchmark/LongFormatterTest.java new file mode 100644 index 0000000..3c2854e --- /dev/null +++ b/app/src/test/java/de/uni_marburg/powersort/benchmark/LongFormatterTest.java @@ -0,0 +1,57 @@ +package de.uni_marburg.powersort.benchmark; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LongFormatterTest { + @Test + void negDigit4() { + assertEquals("-1_234", LongFormatter.formatUnderscore(-1234)); + } + @Test + void negDigit3() { + assertEquals("-123", LongFormatter.formatUnderscore(-123)); + } + @Test + void negDigit2() { + assertEquals("-12", LongFormatter.formatUnderscore(-12)); + } + @Test + void negDigit1() { + assertEquals("-1", LongFormatter.formatUnderscore(-1)); + } + @Test + void digit1() { + assertEquals("0", LongFormatter.formatUnderscore(0)); + } + + @Test + void digit2() { + assertEquals("12", LongFormatter.formatUnderscore(12)); + } + + @Test + void digit3() { + assertEquals("123", LongFormatter.formatUnderscore(123)); + } + + @Test + void digit4() { + assertEquals("1_234", LongFormatter.formatUnderscore(1234)); + } + + @Test + void digit5() { + assertEquals("12_345", LongFormatter.formatUnderscore(12345)); + } + + @Test + void digit6() { + assertEquals("123_456", LongFormatter.formatUnderscore(123456)); + } + @Test + void digit7() { + assertEquals("1_234_567", LongFormatter.formatUnderscore(1234567)); + } +}