progs/catastrophic.java
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 07 Oct 2016 22:08:03 +0100
changeset 444 3056a4c071b0
parent 420 25bc57b32efa
child 474 4bdf0dedd708
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
     1
// a case of catastrophic backtracking in Java
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
     2
//
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
     3
// regexp: (a*)*b
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
     4
// strings: aa....
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
     5
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import java.util.regex.*;
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
public class catastrophic {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
    public static void main(String[] args) {
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    10
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    11
        //we always run all the tests twice -> warmup of the JVM
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
        for (int runs = 0; runs < 2; runs++) {
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    13
            
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    14
            Pattern pattern = Pattern.compile("(a*)*b");
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    15
            
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    16
            // Run from 5 to 28 characters
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    17
            for (int length = 5; length < 28; length++) {
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    18
                
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
                // Build input of specified length
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
                String input = "";
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    21
                for (int i = 0; i < length; i++) { input += "a"; }
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
                
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
                // Measure the average duration of two calls...  
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
                long start = System.nanoTime();
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
                for (int i = 0; i < 2; i++) {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
                    pattern.matcher(input).find();
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
                }
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    28
                
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    29
                System.out.println(length + " " + input + ": " 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    30
                       + ((System.nanoTime() - start) / 2000000000d) 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    31
                       + "s");
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
            }
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
        }
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    }
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    35
}