I'm facing a dilemma. I'm parsing a string and can either do
s.matches(regex)
or I can do
s.startsWith(..) && s.endsWith(..)
As you already realize, its not a complicated regexp, and both situations will work. The idea is, the string may be very long (hundreds of chars), so I wish to maximize efficiency. What works how and better suits the issue?
Here's a really rather crude benchmark to give you an idea. Adapt it to your use cases to give you more relevant results. startsWith and endsWith are much faster. Results after 1000000 runs:
uncompiled pattern 1091ms
compiled pattern 745ms
startsWith/endsWith 24ms
public class TestRegex {
String regex = "^start.*end$";
Pattern p = Pattern.compile(regex);
String start = "start";
String end = "end";
String search = start + "fewbjlhfgljghfadsjhfdsaglfdhjgahfgfjkhgfdkhjsagafdskghjafdkhjgfadskhjgfdsakhjgfdaskhjgafdskjhgafdsjhkgfads" +end;
int runs = 1000000;
@Test
public final void test() {
//init run
for (int i=0;i<runs;i++)
search.matches(regex);
for (int i=0;i<runs;i++)
p.matcher(search).matches();
for (int i=0;i<runs;i++){
search.startsWith(start);
search.endsWith(end);
}
//timed run;
Stopwatch s = Stopwatch.createStarted();
for (int i=0;i<runs;i++)
search.matches(regex);
System.out.println(s.elapsed(TimeUnit.MILLISECONDS));
s.reset(); s.start();
for (int i=0;i<runs;i++)
p.matcher(search).matches();
System.out.println(s.elapsed(TimeUnit.MILLISECONDS));
s.reset(); s.start();
for (int i=0;i<runs;i++){
search.startsWith(start);
search.endsWith(end);
}
System.out.println(s.elapsed(TimeUnit.MILLISECONDS));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With