diff --git a/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/performance/BatchConcatBenchmark.java b/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/performance/BatchConcatBenchmark.java new file mode 100644 index 0000000000..e834777761 --- /dev/null +++ b/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/performance/BatchConcatBenchmark.java @@ -0,0 +1,138 @@ +package com.baeldung.performance; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@State(Scope.Benchmark) +@BenchmarkMode(Mode.AverageTime) +@Warmup(iterations = 1, time = 2, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Fork(value = 1) +public class BatchConcatBenchmark { + + private static final String TOKEN = "string"; + private static final String[] DATA_100 = prepareData(100); + private static final String[] DATA_1000 = prepareData(1000); + private static final String[] DATA_10000 = prepareData(10000); + + private static final String FORMAT_STR_100 = getFormatStr(100); + private static final String FORMAT_STR_1000 = getFormatStr(1000); + private static final String FORMAT_STR_10000 = getFormatStr(10000); + + private static String[] prepareData(int size) { + String[] data = new String[size]; + for (int n=0;n strList = List.of(data); + concatString = strList.stream().collect(Collectors.joining("")); + blackhole.consume(concatString); + } + + public static void main(String[] args) throws Exception { + Options options = new OptionsBuilder() + .include(BatchConcatBenchmark.class.getSimpleName()).threads(1) + .shouldFailOnError(true) + .shouldDoGC(true) + .jvmArgs("-server").build(); + new Runner(options).run(); + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/performance/LoopConcatBenchmark.java b/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/performance/LoopConcatBenchmark.java new file mode 100644 index 0000000000..198754e57d --- /dev/null +++ b/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/performance/LoopConcatBenchmark.java @@ -0,0 +1,211 @@ +package com.baeldung.performance; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringJoiner; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@State(Scope.Benchmark) +@BenchmarkMode(Mode.AverageTime) +@Warmup(iterations = 1, time = 2, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Fork(value = 1) +public class LoopConcatBenchmark { + + private static final String TOKEN = "string"; + private static final int ITERATION_100 = 100; + private static final int ITERATION_1000 = 1000; + private static final int ITERATION_10000 = 10000; + + @Benchmark + public static void concatByPlusBy100(Blackhole blackhole) { + concatByPlus(ITERATION_100, blackhole); + } + + @Benchmark + public static void concatByPlusBy1000(Blackhole blackhole) { + concatByPlus(ITERATION_1000, blackhole); + } + + @Benchmark + public static void concatByPlusBy10000(Blackhole blackhole) { + concatByPlus(ITERATION_10000, blackhole); + } + + public static void concatByPlus(int iterations, Blackhole blackhole) { + String concatString = ""; + for (int n=0;n strList = new ArrayList<>(); + strList.add(concatString); + strList.add(TOKEN); + for (int n=0; n