Josh Cummings suggestions: JMH documentation states that benchmark methods should return a value in order to defend against Dead Code Elimination
This commit is contained in:
parent
2d6be72eab
commit
9c512c4a82
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.patternreuse;
|
package com.baeldung.patternreuse;
|
||||||
|
|
||||||
import org.openjdk.jmh.annotations.*;
|
import org.openjdk.jmh.annotations.*;
|
||||||
|
import org.openjdk.jmh.infra.Blackhole;
|
||||||
import org.openjdk.jmh.runner.RunnerException;
|
import org.openjdk.jmh.runner.RunnerException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -29,52 +30,52 @@ public class PatternPerformanceComparison {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void matcherFromPreCompiledPatternResetMatches() {
|
public void matcherFromPreCompiledPatternResetMatches(Blackhole bh) {
|
||||||
//With pre-compiled pattern and reusing the matcher
|
//With pre-compiled pattern and reusing the matcher
|
||||||
// 1 Pattern object created
|
// 1 Pattern object created
|
||||||
// 1 Matcher objects created
|
// 1 Matcher objects created
|
||||||
for (String value : values) {
|
for (String value : values) {
|
||||||
matcherFromPreCompiledPattern.reset(value).matches();
|
bh.consume(matcherFromPreCompiledPattern.reset(value).matches());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void preCompiledPatternMatcherMatches() {
|
public void preCompiledPatternMatcherMatches(Blackhole bh) {
|
||||||
// With pre-compiled pattern
|
// With pre-compiled pattern
|
||||||
// 1 Pattern object created
|
// 1 Pattern object created
|
||||||
// 5_000_000 Matcher objects created
|
// 5_000_000 Matcher objects created
|
||||||
for (String value : values) {
|
for (String value : values) {
|
||||||
preCompiledPattern.matcher(value).matches();
|
bh.consume(preCompiledPattern.matcher(value).matches());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void patternCompileMatcherMatches() {
|
public void patternCompileMatcherMatches(Blackhole bh) {
|
||||||
// Above approach "Pattern.matches(PATTERN, value)" makes this internally
|
// Above approach "Pattern.matches(PATTERN, value)" makes this internally
|
||||||
// 5_000_000 Pattern objects created
|
// 5_000_000 Pattern objects created
|
||||||
// 5_000_000 Matcher objects created
|
// 5_000_000 Matcher objects created
|
||||||
for (String value : values) {
|
for (String value : values) {
|
||||||
Pattern.compile(PATTERN).matcher(value).matches();
|
bh.consume(Pattern.compile(PATTERN).matcher(value).matches());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void patternMatches() {
|
public void patternMatches(Blackhole bh) {
|
||||||
// Above approach "value.matches(PATTERN)" makes this internally
|
// Above approach "value.matches(PATTERN)" makes this internally
|
||||||
// 5_000_000 Pattern objects created
|
// 5_000_000 Pattern objects created
|
||||||
// 5_000_000 Matcher objects created
|
// 5_000_000 Matcher objects created
|
||||||
for (String value : values) {
|
for (String value : values) {
|
||||||
Pattern.matches(PATTERN, value);
|
bh.consume(Pattern.matches(PATTERN, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void stringMatchs() {
|
public void stringMatchs(Blackhole bh) {
|
||||||
// 5_000_000 Pattern objects created
|
// 5_000_000 Pattern objects created
|
||||||
// 5_000_000 Matcher objects created
|
// 5_000_000 Matcher objects created
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
for (String value : values) {
|
for (String value : values) {
|
||||||
value.matches(PATTERN);
|
bh.consume(value.matches(PATTERN));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue