I have the following function.
private boolean codeContains(String name, String code) {
if (name == null || code == null) {
return false;
}
Pattern pattern = Pattern.compile("\\b" + Pattern.quote(name) + "\\b");
Matcher matcher = pattern.matcher(code);
return matcher.find();
}
It is called many thousand times in my code, and is the function in which my program spends the most amount of time in. Is there any way to make this function go faster, or is it already as fast as it can be?
If you don’t need to check word boundaries, you might do this :
If you need to check word boundaries but, as I suppose is your case, you have a big
codein which you often search, you could “compile” thecodeonce bycodestring using the split methodOf course, if you have more than one code, it’s easy to store them in a structure adapted to your program, for example in a map having as key the file name.