What's the difference between String.matches and Matcher.matches? Is there any difference in terms of performance or other things?
Absolutely. A Matcher
is created on on a precompiled regexp, while String.matches
must recompile the regexp every time it executes, so it becomes more wasteful the more often you run that line of code.
String.matches internally delegates to Matcher.matches.
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
public static boolean matches(String regex, CharSequence input) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}
If you are reusing the Pattern object then there will be some performance benefit. Also when using Pattern/Matcher you can group your regular expressions and get the matching parts.
The bottomline is if you have a regex that you will use only once and you don't need to parse your string to get matching parts then use either. But if you are going to use the same regex against multiple strings or you need parts of the String based on regex create a Pattern and get Matcher using it.
Out of curiosity I did this small test on the time differences. Turns out that using a pre-compiled pattern is more than 5 times faster than using String.matches method.
import java.util.regex.Pattern;
/**
* @author Rajind Ruparathna
*/
public class MatchesTest {
public static void main(String Args[]) {
String first = "@\\{message.headers\\.?([^\\}]*)\\}";
String second = "@\\{message.headers.wolla\\}";
long start, end, total;
float avg;
int NUM_OF_ITERATIONS = 100;
Pattern pattern = Pattern.compile(first);
total = 0;
start = 0;
end = 0;
avg = 0;
for (int i=0; i< NUM_OF_ITERATIONS; i++) {
start = System.nanoTime();
pattern.matcher(second).matches();
end = System.nanoTime();
total = total + (end - start);
}
avg = total/NUM_OF_ITERATIONS;
System.out.println("Duration pre compiled: " + avg);
total = 0;
start = 0;
end = 0;
avg = 0;
for (int i=0; i< NUM_OF_ITERATIONS; i++) {
start = System.nanoTime();
first.matches(second);
end = System.nanoTime();
total = total + (end - start);
}
avg = total/NUM_OF_ITERATIONS;
System.out.println("In place compiled: " + avg);
}
}
Output (nanoseconds):
Duration pre compiled: 4505.0
In place compiled: 44960.0
P.S. This test is a quick and dirty test and may not be according to performance benchmarking practises. If you want to get highly accurate results please use a micro benchmarking tool.
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