progs/catastrophic.java
author Christian Urban <urbanc@in.tum.de>
Mon, 22 Aug 2016 09:12:03 +0200
changeset 412 1cef3924f7a2
parent 411 1aec0e1fda86
child 420 25bc57b32efa
permissions -rw-r--r--
updated

import java.util.regex.*;

public class catastrophic {
    public static void main(String[] args) {
        for (int runs = 0; runs < 2; runs++) {
            //Pattern pattern = Pattern.compile("(0*)*A");
            //Pattern pattern = Pattern.compile("a?{20}a{20}");

            // Run from 5 to 25 characters
            for (int length = 5; length < 30; length++) {
                Pattern pattern = Pattern.compile("(a*)*b");
                // Build input of specified length
                String input = "";
                for (int i = 0; i < length; i++) { input += "a"; }
                
                // Measure the average duration of two calls...  
                long start = System.nanoTime();
                for (int i = 0; i < 2; i++) {
                    pattern.matcher(input).find();
                }
                System.out.println(length + " " + input + ": " 
                       + ((System.nanoTime() - start) / 2000000000d) 
                       + "s");
            }
        }
    }
}