BASE-4618: Use JMH benchmark tool

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

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