From c8b3ecbd36ff1c5192e1a67673145726354c44d2 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Sat, 6 Mar 2021 10:00:01 +0100 Subject: [PATCH] BASE-4618: Use JMH benchmark tool --- core-java-modules/core-java-lang-4/pom.xml | 18 ++++++++++ .../finalkeyword/BenchmarkRunner.java | 34 +++++++++++++++++++ .../finalkeyword/LocalVariableFinal.java | 22 ------------ .../finalkeyword/LocalVariableNonFinal.java | 22 ------------ 4 files changed, 52 insertions(+), 44 deletions(-) create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/BenchmarkRunner.java delete mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/LocalVariableFinal.java delete mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/LocalVariableNonFinal.java diff --git a/core-java-modules/core-java-lang-4/pom.xml b/core-java-modules/core-java-lang-4/pom.xml index 520de40529..3e92e9f9c7 100644 --- a/core-java-modules/core-java-lang-4/pom.xml +++ b/core-java-modules/core-java-lang-4/pom.xml @@ -16,6 +16,20 @@ ../ + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + test + + + core-java-lang-4 @@ -25,5 +39,9 @@ + + + 1.28 + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/BenchmarkRunner.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/BenchmarkRunner.java new file mode 100644 index 0000000000..ee34114195 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/BenchmarkRunner.java @@ -0,0 +1,34 @@ +package com.baeldung.finalkeyword; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; + +import java.util.concurrent.TimeUnit; + +public class BenchmarkRunner { + + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(args); + } + + @Benchmark + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public static String concatNonFinalStrings() { + String x = "x"; + String y = "y"; + return x + y; + } + + @Benchmark + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public static String concatFinalStrings() { + final String x = "x"; + final String y = "y"; + return x + y; + } + +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/LocalVariableFinal.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/LocalVariableFinal.java deleted file mode 100644 index 74f454508f..0000000000 --- a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/LocalVariableFinal.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.finalkeyword; - -public class LocalVariableFinal { - - public static void main(String[] args) { - for (int i = 0; i < 1500; i++) { - long startTime = System.nanoTime(); - String result = concatStrings(); - long totalTime = System.nanoTime() - startTime; - if (i >= 500) { - System.out.println(totalTime); - } - } - } - - private static String concatStrings() { - final String x = "x"; - final String y = "y"; - return x + y; - } - -} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/LocalVariableNonFinal.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/LocalVariableNonFinal.java deleted file mode 100644 index 14b8401d76..0000000000 --- a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/finalkeyword/LocalVariableNonFinal.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.finalkeyword; - -public class LocalVariableNonFinal { - - public static void main(String[] args) { - for (int i = 0; i < 1500; i++) { - long startTime = System.nanoTime(); - String result = concatStrings(); - long totalTime = System.nanoTime() - startTime; - if (i >= 500) { - System.out.println(totalTime); - } - } - } - - private static String concatStrings() { - String x = "x"; - String y = "y"; - return x + y; - } - -}