author | Christian Urban <urbanc@in.tum.de> |
Sun, 13 Oct 2019 00:53:20 +0100 | |
changeset 654 | fb6192488b91 |
parent 616 | 24bbe4e4b37b |
permissions | -rw-r--r-- |
558 | 1 |
// A case of catastrophic backtracking in Java 8 |
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 | 6 |
// |
7 |
// compile: javac catastrophic.java |
|
8 |
// call with: java catastrophic |
|
9 |
// |
|
10 |
// IMPORTANT: |
|
11 |
// Java 9 improved its regex matching engine. |
|
12 |
// This example is now much faster. |
|
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 | 19 |
|
474 | 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 | 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 | 37 |
|
38 |
// Print out time |
|
412 | 39 |
System.out.println(length + " " + input + ": " |
40 |
+ ((System.nanoTime() - start) / 2000000000d) |
|
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 |
} |