Compile regular expressions once
Pattern objects compile the regular expressions which are passed to them. (The compilation happens in memory.) If a regular expression is used many times, then this compilation should be performed only once.
Be wary of the these convenience methods, which compile a regular expression each time they are called:
- Pattern.matches
- String.matches
- String.replaceAll
- String.replaceFirst
- String.split
Here's a comparison of the performance of String.matches versus the use of Pattern and Matcher.
An example run gives :
Time for String: 70 ms
Time for Pattern: 10 ms
import java.util.*; import java.io.*; import java.util.regex.*; public final class FindMatch { /** * Count the number of times the word "assert" occurs in a file. * * Compare the execution times of two techniques. */ public static void main (String... aArguments){ FindMatch findMatch = new FindMatch("assert"); File file = new File("C:\\Temp\\InheritanceInterpreter.java"); Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); findMatch.countMatchesUsingString(file); stopwatch.stop(); log("Time for String: " + stopwatch); stopwatch.start(); findMatch.countMatchesUsingPattern(file); stopwatch.stop(); log("Time for Pattern: " + stopwatch); } public FindMatch(String aTarget){ fTargetWord = aTarget; fPattern = Pattern.compile(fTargetWord); } public int countMatchesUsingString(File aFile){ int result = 0; try { Scanner scanner = new Scanner(aFile); while ( scanner.hasNextLine() ){ String line = scanner.nextLine(); if( line.matches(fTargetWord) ){ ++ result; } } } catch (FileNotFoundException ex) { log(ex); } return result; } public int countMatchesUsingPattern(File aFile) { int result = 0; Matcher matcher = fPattern.matcher(""); try { Scanner scanner = new Scanner(aFile); while ( scanner.hasNextLine() ){ String line = scanner.nextLine(); matcher.reset( line ); //reset the input each time if ( ! matcher.find() ) { ++result; } } } catch (FileNotFoundException ex) { log(ex); } return result; } // PRIVATE // private final String fTargetWord; private final Pattern fPattern; private static void log(Object aObject){ System.out.println(aObject); } }
See Also :
Would you use this technique?
|
|