progs/catastrophic9.java
author Christian Urban <urbanc@in.tum.de>
Thu, 26 Sep 2019 11:00:17 +0100
changeset 633 e4889da2fe29
parent 615 e722f4ba54de
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// A case of catastrophic backtracking in Java 9+
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//------------------------------------------------
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
//
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// It is actually not too bad in comparison what Python
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
// and Java 8 are to compute. But it is still pretty
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// bad, even in Java 9, considering that the the basic
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// part of the CW improves on this by a factor of 100
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// (...if implemented correctly).
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
//
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// regexp: (a*)*b
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
// strings: aa....aaa
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
//
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
// compile with: javac catastrophic9.java
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// call with:    java catastrophic9
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
//
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
//
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
import java.util.regex.*;
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
public class catastrophic9 {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
    public static void main(String[] args) {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
        //we always run all the tests twice -> to warmup of the JVM
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
        for (int runs = 0; runs < 2; runs++) {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
            
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
            Pattern pattern = Pattern.compile("(a*)*b");
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
            
615
e722f4ba54de updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
    28
            // Run from 0 to 50000 characters
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
            for (int length = 0; length < 50000; length += 5000) {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
                
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
                // Build input of specified length
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
                String input = "";
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
                for (int i = 0; i < length; i++) { input += "a"; }
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
                
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
                // Measure the average duration of two calls...  
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
                long start = System.nanoTime();
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
                for (int i = 0; i < 2; i++) {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
                    pattern.matcher(input).find();
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
                }
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
                // Print out time
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
                System.out.println(length + " a's : " 
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
                       + ((System.nanoTime() - start) / 2000000000d) 
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
                       + " secs");
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
            }
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
        }
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    }
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
}