import java.util.regex.*; public class test2 { public static void main(String [] args) { Matcher regex1 = Pattern.compile("^(b|a)*$").matcher(""); Matcher regex2 = Pattern.compile("^(a+|b)*$").matcher(""); int timesToDo = 1000; int noOfChars = 100; int multiplier = 2; int noOfExec = 10; String inputString = "aaaaaaaaab"; for(int j = 0; j < noOfExec; j++){ StringBuffer temp = new StringBuffer(); noOfChars = noOfChars * multiplier; for (int i = 0; i < noOfChars; i++) temp.append(inputString); String testString = temp.toString(); long count = timesToDo; long startTime = System.currentTimeMillis(); while (--count > 0) regex1.reset(testString).find(); double diffSeconds = (System.currentTimeMillis() - startTime)/1000.0; count = timesToDo; startTime = System.currentTimeMillis(); while (--count > 0) regex2.reset(testString).find(); double seconds = (System.currentTimeMillis() - startTime)/1000.0; System.out.println(noOfChars+";"+diffSeconds+";"+seconds); } } }