progs/catastrophic.java
author Christian Urban <urbanc@in.tum.de>
Sun, 21 Aug 2016 18:15:53 +0200
changeset 411 1aec0e1fda86
child 412 1cef3924f7a2
permissions -rw-r--r--
updated catastrophic examples
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
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
            for (int length = 5; length < 25; length++) {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
                Pattern pattern = Pattern.compile("(0*)*A");
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 = "";
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
                for (int i = 0; i < length; i++) { input += "0"; }
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
                }
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
                System.out.println(input + ": " 
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
                       + ((System.nanoTime() - start) / 2000000d) 
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
                       + "ms");
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
}