//上代码
public static void main(String[] args) { int count = 10000000; test1(count); test2(count); } private static void test1(int count) { long l = System.currentTimeMillis(); for (int i = 0; i < count; i++) { new PathPatternParser().parse("permission/test/{id}/test/{age}/{name}").matches(PathContainer.parsePath("permission/test/1/test/30/zhangsan")); } long e = System.currentTimeMillis(); System.out.println(count+", PathPatternParser use totalTime:"+(e-l)); } private static void test2(int count) { long l = System.currentTimeMillis(); for (int i = 0; i < count; i++) { new AntPathMatcher().match("permission/test/{id}/test/{age}/{name}","permission/test/1/test/30/zhangsan"); } long e = System.currentTimeMillis(); System.out.println(count+", AntPathMatcher use totalTime:"+(e-l)); }
性能差异:
10000000, PathPatternParser use totalTime:15977 10000000, AntPathMatcher use totalTime:69457
测试方案2:
static AntPathMatcher antPathMatcher = new AntPathMatcher(); static PathPatternParser pathPatternParser = new PathPatternParser(); public static void main(String[] args) { int count = 1000000; test2(count); test1(count); } private static void test1(int count) { long l = System.currentTimeMillis(); for (int i = 0; i < count; i++) { pathPatternParser.parse("permission/test/{id}/test/{age}/{name}").matches(PathContainer.parsePath("permission/test/1/test/30/zhangsan"+i)); } long e = System.currentTimeMillis(); System.out.println(count+", PathPatternParser use totalTime:"+(e-l)); } private static void test2(int count) { long l = System.currentTimeMillis(); for (int i = 0; i < count; i++) { antPathMatcher.match("permission/test/{id}/test/{age}/{name}","permission/test/1/test/30/zhangsan"+i); // System.out.println(antPathMatcher.match("permission/test/{id}/test/{age}/{name}","permission/test/1/test/30/zhangsan"+i));; } long e = System.currentTimeMillis(); System.out.println(count+", AntPathMatcher use totalTime:"+(e-l)); }
1000000, AntPathMatcher use totalTime:1217 1000000, PathPatternParser use totalTime:1537、
结论:
建议使用AntPathMatcher 应为如果在匹配路径相同的情况下,AntPathMatcher具有更好的性能。并且有缓存。