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