progs/catastrophic.java
author Christian Urban <urbanc@in.tum.de>
Tue, 20 Sep 2016 12:24:29 +0100
changeset 422 5deefcc8cffa
parent 412 1cef3924f7a2
child 420 25bc57b32efa
permissions -rw-r--r--
updated programs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
import java.util.regex.*;
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
public class catastrophic {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
    public static void main(String[] args) {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
        for (int runs = 0; runs < 2; runs++) {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
            //Pattern pattern = Pattern.compile("(0*)*A");
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
            //Pattern pattern = Pattern.compile("a?{20}a{20}");
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
            // Run from 5 to 25 characters
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    10
            for (int length = 5; length < 30; length++) {
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    11
                Pattern pattern = Pattern.compile("(a*)*b");
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
                // Build input of specified length
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
                String input = "";
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    14
                for (int i = 0; i < length; i++) { input += "a"; }
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
                
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
                // Measure the average duration of two calls...  
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
                long start = System.nanoTime();
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
                for (int i = 0; i < 2; i++) {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
                    pattern.matcher(input).find();
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
                }
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    21
                System.out.println(length + " " + input + ": " 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    22
                       + ((System.nanoTime() - start) / 2000000000d) 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    23
                       + "s");
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
            }
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
        }
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    }
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
}