Skip to content

Commit f55e379

Browse files
fscorromaibin
authored andcommitted
optimizing regex expressions (eugenp#4961)
1 parent ce8d193 commit f55e379

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.regexp.datepattern.optmization;
2+
3+
public class OptimizedMatcher {
4+
5+
6+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.baeldung.regexp.optmization;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Random;
10+
import java.util.regex.Matcher;
11+
import java.util.regex.Pattern;
12+
13+
import static org.junit.Assert.assertTrue;
14+
15+
public class OptimizedMatcherUnitTest {
16+
17+
private long time;
18+
private long mstimePreCompiled;
19+
private long mstimeNotPreCompiled;
20+
21+
private String action;
22+
23+
private List<String> items;
24+
25+
@Before
26+
public void setup() {
27+
Random random = new Random();
28+
items = new ArrayList<String>();
29+
long average = 0;
30+
31+
for (int i = 0; i < 100000; ++i) {
32+
StringBuilder s = new StringBuilder();
33+
int characters = random.nextInt(7) + 1;
34+
for (int k = 0; k < characters; ++ k) {
35+
char c = (char)(random.nextInt('Z' - 'A') + 'A');
36+
int rep = random.nextInt(95) + 5;
37+
for (int j = 0; j < rep; ++ j)
38+
s.append(c);
39+
average += rep;
40+
}
41+
items.add(s.toString());
42+
}
43+
44+
average /= 100000;
45+
System.out.println("generated data, average length: " + average);
46+
}
47+
48+
49+
@Test
50+
public void givenANotPreCompiledAndAPreCompiledPatternA_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
51+
52+
testPatterns("A*");
53+
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
54+
}
55+
56+
@Test
57+
public void givenANotPreCompiledAndAPreCompiledPatternABC_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
58+
59+
testPatterns("A*B*C*");
60+
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
61+
}
62+
63+
@Test
64+
public void givenANotPreCompiledAndAPreCompiledPatternECWF_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
65+
66+
testPatterns("E*C*W*F*");
67+
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
68+
}
69+
70+
private void testPatterns(String regex) {
71+
time = System.nanoTime();
72+
int matched = 0;
73+
int unmatched = 0;
74+
75+
for (String item : this.items) {
76+
if (item.matches(regex)) {
77+
++matched;
78+
}
79+
else {
80+
++unmatched;
81+
}
82+
}
83+
84+
this.action = "uncompiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
85+
86+
this.mstimeNotPreCompiled = (System.nanoTime() - time) / 1000000;
87+
System.out.println(this.action + ": " + mstimeNotPreCompiled + "ms");
88+
89+
time = System.nanoTime();
90+
91+
Matcher matcher = Pattern.compile(regex).matcher("");
92+
matched = 0;
93+
unmatched = 0;
94+
95+
for (String item : this.items) {
96+
if (matcher.reset(item).matches()) {
97+
++matched;
98+
}
99+
else {
100+
++unmatched;
101+
}
102+
}
103+
104+
this.action = "compiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
105+
106+
this.mstimePreCompiled = (System.nanoTime() - time) / 1000000;
107+
System.out.println(this.action + ": " + mstimePreCompiled + "ms");
108+
}
109+
}

0 commit comments

Comments
 (0)