benchmark preparations

This commit is contained in:
Daniel Langbein 2024-12-10 11:00:11 +00:00
parent 4d9acbf6ba
commit 8e20550b44
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
6 changed files with 107 additions and 9 deletions

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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.";
}
}

View File

@ -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.";
}
}

View File

@ -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));
}
}