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