BASE-4618: Use JMH benchmark tool

This commit is contained in:
Daniel Strmecki 2021-03-06 10:00:01 +01:00
parent f0ee9ddf02
commit c8b3ecbd36
4 changed files with 52 additions and 44 deletions

View File

@ -16,6 +16,20 @@
<relativePath>../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-lang-4</finalName>
<resources>
@ -25,5 +39,9 @@
</resource>
</resources>
</build>
<properties>
<jmh.version>1.28</jmh.version>
</properties>
</project>

View File

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

View File

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

View File

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