progs/catastrophic.java
changeset 411 1aec0e1fda86
child 412 1cef3924f7a2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/catastrophic.java	Sun Aug 21 18:15:53 2016 +0200
@@ -0,0 +1,27 @@
+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 < 25; length++) {
+                Pattern pattern = Pattern.compile("(0*)*A");
+                // Build input of specified length
+                String input = "";
+                for (int i = 0; i < length; i++) { input += "0"; }
+                
+                // 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(input + ": " 
+                       + ((System.nanoTime() - start) / 2000000d) 
+                       + "ms");
+            }
+        }
+    }
+} 
\ No newline at end of file