progs/catastrophic.java
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 11 Jan 2017 14:46:37 +0000
changeset 101 139eb1ed2d57
parent 70 6024381415cb
child 163 84917f2e16cd
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
70
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// a case of catastrophic backtracking in Java
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
// regexp: (a*)*b
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// strings: aa....
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import java.util.regex.*;
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
public class catastrophic {
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
    public static void main(String[] args) {
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
        //we always run all the tests twice -> warmup of the JVM
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
        for (int runs = 0; runs < 2; runs++) {
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
            
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
            Pattern pattern = Pattern.compile("(a*)*b");
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
            
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
            // Run from 5 to 28 characters
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
            for (int length = 5; length < 28; length++) {
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
                
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
                // Build input of specified length
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
                String input = "";
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
                for (int i = 0; i < length; i++) { input += "a"; }
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
                
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
                // Measure the average duration of two calls...  
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
                long start = System.nanoTime();
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
                for (int i = 0; i < 2; i++) {
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
                    pattern.matcher(input).find();
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
                }
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
                
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
                System.out.println(length + " " + input + ": " 
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
                       + ((System.nanoTime() - start) / 2000000000d) 
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
                       + "s");
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
            }
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
        }
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    }
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
} 
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// javac catastrophic.java 
6024381415cb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
// java catastrophic