progs/catastrophic.java
changeset 411 1aec0e1fda86
child 412 1cef3924f7a2
equal deleted inserted replaced
410:3e1b8266ea65 411:1aec0e1fda86
       
     1 import java.util.regex.*;
       
     2 
       
     3 public class catastrophic {
       
     4     public static void main(String[] args) {
       
     5         for (int runs = 0; runs < 2; runs++) {
       
     6             //Pattern pattern = Pattern.compile("(0*)*A");
       
     7             //Pattern pattern = Pattern.compile("a?{20}a{20}");
       
     8 
       
     9             // Run from 5 to 25 characters
       
    10             for (int length = 5; length < 25; length++) {
       
    11                 Pattern pattern = Pattern.compile("(0*)*A");
       
    12                 // Build input of specified length
       
    13                 String input = "";
       
    14                 for (int i = 0; i < length; i++) { input += "0"; }
       
    15                 
       
    16                 // Measure the average duration of two calls...  
       
    17                 long start = System.nanoTime();
       
    18                 for (int i = 0; i < 2; i++) {
       
    19                     pattern.matcher(input).find();
       
    20                 }
       
    21                 System.out.println(input + ": " 
       
    22                        + ((System.nanoTime() - start) / 2000000d) 
       
    23                        + "ms");
       
    24             }
       
    25         }
       
    26     }
       
    27 }