From 01e57c998e0bdde8e65b07cc07bc6facf49014ff Mon Sep 17 00:00:00 2001 From: HoaPhan Date: Wed, 8 Nov 2023 12:04:58 +1100 Subject: [PATCH] JDK 8 11 17 21 regexp perf --- vs/java/regexp/Readme.md | 50 ++++++++++++++++++++++++++++++++++++++++ vs/java/regexp/Reg.java | 28 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 vs/java/regexp/Readme.md create mode 100644 vs/java/regexp/Reg.java diff --git a/vs/java/regexp/Readme.md b/vs/java/regexp/Readme.md new file mode 100644 index 00000000..cabc7675 --- /dev/null +++ b/vs/java/regexp/Readme.md @@ -0,0 +1,50 @@ + +### 8.332.08.1-amzn, java 11.0.16-amzn, java 17.0.8-amzn, java 21-graalce +``` +sdk use java 8.332.08.1-amzn +javac Reg.java && java Reg +time: false [22197 ms] + +sdk use java 11.0.16-amzn +javac Reg.java && java Reg +time: false [5 ms] + + +sdk use java 17.0.8-amzn +javac Reg.java && java Reg +time: false [2 ms] + + +sdk use java 21-graalce +javac Reg.java && java Reg +time: false [0 ms] + +``` + +### 21 Amaz vs 21 Graal native (nanosec scale) + +``` +Using java version 21-amzn in this shell. +[2295042 ns] +[2043833 ns] +[2035208 ns] +[2039458 ns] +[2123292 ns] +[2206541 ns] + +graalvm native +[115458 ns] +[140333 ns] +[101666 ns] +[122791 ns] +[116959 ns] +[177125 ns] +[119584 ns] +``` + +### ref +https://kean.blog/post/regex-matcher +https://kean.blog/post/regex-matcher +https://algs4.cs.princeton.edu/54regexp/ +https://querix.com/go/lycia/Content/06_reference/4gl/regular_expressions/regex_algorithms.htm +https://sedgewick.io/wp-content/uploads/2022/04/Algs20-RegularExpressions.pdf \ No newline at end of file diff --git a/vs/java/regexp/Reg.java b/vs/java/regexp/Reg.java new file mode 100644 index 00000000..e28871de --- /dev/null +++ b/vs/java/regexp/Reg.java @@ -0,0 +1,28 @@ +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Reg { + + public static void main(String[] args) { + long startTime = System.nanoTime(); + boolean m = reg(); + long endTime = System.nanoTime(); + long durationMs = (endTime - startTime)/1000000; + + System.out.println(String.format("time: %s [%s ms]" , m ,durationMs)); + } + + public static String reverseString(String str) { + if (str.isEmpty()) + return str; + return reverseString(str.substring(1)) + str.charAt(0); + } + + public static boolean reg(){ + final String expr = "(a|b|ab)*bc"; + final String in = "ababababababababababababababababababababababababababababac"; + Pattern p = Pattern.compile(expr); + Matcher m = p.matcher(in); + return m.matches(); + } +} \ No newline at end of file