Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.regex.PatternSyntaxException;
- public class RegexPerformanceTest
- {
- public static void main(String[] args)
- {
- String regex1 = "(?im)^Hi there (.*$)(?<! is here)";
- String regex2 = "(?im)^Hi there (.*)(?<! is here)$";
- String errorTest = "Hi there John Doe is here";
- String successTest = "Hi there John Doe (jdo)";
- int repetitions = 1000000;
- try
- {
- System.out.println("Regex1, success: " + testRegexPerformance(regex1, successTest, repetitions)); // ~1200
- System.out.println("Regex1, error: " + testRegexPerformance(regex1, errorTest, repetitions)); // ~1200
- System.out.println("Regex2, success: " + testRegexPerformance(regex2, successTest, repetitions)); // ~1100
- System.out.println("Regex2, error: " + testRegexPerformance(regex2, errorTest, repetitions)); // ~1800
- }
- catch(PatternSyntaxException e)
- {
- e.printStackTrace();
- }
- }
- private static long testRegexPerformance(String regex, String subject, int numberOfRepetitions) throws PatternSyntaxException
- {
- boolean foundMatch;
- long endTime = 0;
- long startTime = 0;
- startTime = System.currentTimeMillis();
- for (int i = 0; i < numberOfRepetitions; i++)
- {
- foundMatch = subject.matches(regex);
- }
- endTime = System.currentTimeMillis();
- return endTime - startTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement