progs/catastrophic/catastrophic9.java
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 25 Sep 2023 15:12:11 +0100
changeset 929 9541e073f2ed
parent 906 2bf1516d730f
child 959 64ec1884d860
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
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 741
diff changeset
    23
	
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
        //we always run all the tests twice -> to warmup of the JVM
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
        for (int runs = 0; runs < 2; runs++) {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
            
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
            Pattern pattern = Pattern.compile("(a*)*b");
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
            
615
e722f4ba54de updated
Christian Urban <urbanc@in.tum.de>
parents: 612
diff changeset
    29
            // Run from 0 to 50000 characters
612
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
            for (int length = 0; length < 50000; length += 5000) {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
                
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
                // Build input of specified length
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
                String input = "";
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
                for (int i = 0; i < length; i++) { input += "a"; }
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
                
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
                // Measure the average duration of two calls...  
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
                long start = System.nanoTime();
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
                for (int i = 0; i < 2; i++) {
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
                    pattern.matcher(input).find();
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
                }
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
                // Print out time
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
                System.out.println(length + " a's : " 
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
                       + ((System.nanoTime() - start) / 2000000000d) 
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
                       + " secs");
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
    }
7a12053567d4 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
}