|
1 | | -package com.baeldung.pattern; |
| 1 | +package com.baeldung.patternreuse; |
2 | 2 |
|
3 | | -import java.time.Duration; |
| 3 | +import org.openjdk.jmh.annotations.*; |
| 4 | +import org.openjdk.jmh.runner.RunnerException; |
| 5 | + |
| 6 | +import java.io.IOException; |
4 | 7 | import java.time.Instant; |
5 | 8 | import java.util.ArrayList; |
6 | 9 | import java.util.List; |
| 10 | +import java.util.concurrent.TimeUnit; |
7 | 11 | import java.util.regex.Matcher; |
8 | 12 | import java.util.regex.Pattern; |
9 | 13 |
|
| 14 | +@BenchmarkMode(Mode.AverageTime) |
| 15 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 16 | +@Fork(value = 1, warmups = 1) |
| 17 | +@Warmup(iterations = 5) |
| 18 | +@State(Scope.Benchmark) |
10 | 19 | public class PatternPerformanceComparison { |
11 | 20 |
|
12 | 21 | private static final String PATTERN = "\\d*[02468]"; |
13 | 22 | private static List<String> values; |
14 | 23 |
|
15 | | - public static void main(String[] args) { |
16 | | - loadValues(); |
| 24 | + private static Matcher matcherFromPreCompiledPattern; |
| 25 | + private static Pattern preCompiledPattern; |
17 | 26 |
|
18 | | - // 5_000_000 Pattern objects created |
19 | | - // 5_000_000 Matcher objects created |
20 | | - Instant start = Instant.now(); |
| 27 | + public static void main(String[] args) throws IOException, RunnerException { |
| 28 | + org.openjdk.jmh.Main.main(args); |
| 29 | + } |
| 30 | + |
| 31 | + @Benchmark |
| 32 | + public void matcherFromPreCompiledPatternResetMatches() { |
| 33 | + //With pre-compiled pattern and reusing the matcher |
| 34 | + // 1 Pattern object created |
| 35 | + // 1 Matcher objects created |
21 | 36 | for (String value : values) { |
22 | | - value.matches(PATTERN); |
| 37 | + matcherFromPreCompiledPattern.reset(value).matches(); |
23 | 38 | } |
24 | | - System.out.println(Duration.between(start, Instant.now()).toMillis() + "ms -> String.matchs(regex)"); |
| 39 | + } |
25 | 40 |
|
26 | | - // Above approach "value.matches(PATTERN)" makes this internally |
27 | | - // 5_000_000 Pattern objects created |
| 41 | + @Benchmark |
| 42 | + public void preCompiledPatternMatcherMatches() { |
| 43 | + // With pre-compiled pattern |
| 44 | + // 1 Pattern object created |
28 | 45 | // 5_000_000 Matcher objects created |
29 | | - start = Instant.now(); |
30 | 46 | for (String value : values) { |
31 | | - Pattern.matches(PATTERN, value); |
| 47 | + preCompiledPattern.matcher(value).matches(); |
32 | 48 | } |
33 | | - System.out.println(Duration.between(start, Instant.now()).toMillis() + "ms -> Pattern.matches(regex, charSequence)"); |
| 49 | + } |
34 | 50 |
|
| 51 | + @Benchmark |
| 52 | + public void patternCompileMatcherMatches() { |
35 | 53 | // Above approach "Pattern.matches(PATTERN, value)" makes this internally |
36 | 54 | // 5_000_000 Pattern objects created |
37 | 55 | // 5_000_000 Matcher objects created |
38 | | - start = Instant.now(); |
39 | 56 | for (String value : values) { |
40 | 57 | Pattern.compile(PATTERN).matcher(value).matches(); |
41 | 58 | } |
42 | | - System.out.println(Duration.between(start, Instant.now()).toMillis() + "ms -> Pattern.compile(regex).matcher(charSequence).matches()"); |
| 59 | + } |
43 | 60 |
|
44 | | - // With pre-compiled pattern |
45 | | - // 1 Pattern object created |
| 61 | + @Benchmark |
| 62 | + public void patternMatches() { |
| 63 | + // Above approach "value.matches(PATTERN)" makes this internally |
| 64 | + // 5_000_000 Pattern objects created |
46 | 65 | // 5_000_000 Matcher objects created |
47 | | - Pattern preCompiledPattern = Pattern.compile(PATTERN); |
48 | | - start = Instant.now(); |
49 | 66 | for (String value : values) { |
50 | | - preCompiledPattern.matcher(value).matches(); |
| 67 | + Pattern.matches(PATTERN, value); |
51 | 68 | } |
52 | | - System.out.println(Duration.between(start, Instant.now()).toMillis() + "ms -> preCompiledPattern.matcher(value).matches()"); |
| 69 | + } |
53 | 70 |
|
54 | | - //With pre-compiled pattern and reusing the matcher |
55 | | - // 1 Pattern object created |
56 | | - // 1 Matcher objects created |
57 | | - Matcher matcherFromPreCompiledPattern = preCompiledPattern.matcher(""); |
58 | | - start = Instant.now(); |
| 71 | + @Benchmark |
| 72 | + public void stringMatchs() { |
| 73 | + // 5_000_000 Pattern objects created |
| 74 | + // 5_000_000 Matcher objects created |
| 75 | + Instant start = Instant.now(); |
59 | 76 | for (String value : values) { |
60 | | - matcherFromPreCompiledPattern.reset(value).matches(); |
| 77 | + value.matches(PATTERN); |
61 | 78 | } |
62 | | - System.out.println(Duration.between(start, Instant.now()).toMillis() + "ms -> matcherFromPreCompiledPattern.reset(value).matches()"); |
63 | 79 | } |
64 | 80 |
|
65 | | - private static void loadValues() { |
| 81 | + @Setup() |
| 82 | + public void setUp() { |
| 83 | + preCompiledPattern = Pattern.compile(PATTERN); |
| 84 | + matcherFromPreCompiledPattern = preCompiledPattern.matcher(""); |
| 85 | + |
66 | 86 | values = new ArrayList<>(); |
67 | 87 | for (int x = 1; x <= 5_000_000; x++) { |
68 | 88 | values.add(String.valueOf(x)); |
|
0 commit comments