Here is a really pretty crude test 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 1,000,000 runs:
uncompiled template 1091ms
745ms
/ 24
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() {
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);
}
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));
}
}