progs/catastrophic.java
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 04 Jul 2020 16:58:12 +0100
changeset 732 c7bdd7eac4cb
parent 616 24bbe4e4b37b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     1
// A case of catastrophic backtracking in Java 8
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     2
//-----------------------------------------------
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
     3
//
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
     4
// regexp: (a*)*b
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
     5
// strings: aa....
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     6
//
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     7
// compile:    javac catastrophic.java
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     8
// call with:  java catastrophic
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
     9
//
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
    10
// IMPORTANT: 
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
    11
// Java 9 improved its regex matching engine.
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
    12
// This example is now much faster.
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 474
diff changeset
    13
//
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    14
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
import java.util.regex.*;
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
public class catastrophic {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    public static void main(String[] args) {
616
24bbe4e4b37b updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    19
	
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    20
        //we always run all the tests twice -> to warmup of the JVM
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
        for (int runs = 0; runs < 2; runs++) {
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    22
            
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    23
            Pattern pattern = Pattern.compile("(a*)*b");
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    24
            
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    25
            // Run from 5 to 28 characters
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    26
            for (int length = 5; length < 28; length++) {
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    27
                
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
                // Build input of specified length
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
                String input = "";
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    30
                for (int i = 0; i < length; i++) { input += "a"; }
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
                
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
                // Measure the average duration of two calls...  
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
                long start = System.nanoTime();
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
                for (int i = 0; i < 2; i++) {
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
                    pattern.matcher(input).find();
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
                }
474
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    37
4bdf0dedd708 updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    38
                // Print out time
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    39
                System.out.println(length + " " + input + ": " 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    40
                       + ((System.nanoTime() - start) / 2000000000d) 
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 411
diff changeset
    41
                       + "s");
411
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
            }
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
        }
1aec0e1fda86 updated catastrophic examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    }
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 412
diff changeset
    45
}