记一个.parallelStream()效率优于.stream().parallel()
业务背景:数据库5519条数据,同步接口传来6978个sku,要对比找出数据库缺少的sku。 ps:为了排除逻辑错误导致的时间差异,两边循环完毕后用set.addAll的size确保了结果一致。 for循环
private List<String> forMethod(List<Map> oldProductList, List<String> newList) { List<String> addList = new ArrayList<>(); for (String productCode : newList) { boolean exist = false; for (Map map : oldProductList) { if (productCode.equals(map.get("sku"))) { exist = true; break; } } if (!exist) { addList.add(productCode); } } return addList; }
stream版。
List<String> addList = newList.stream().filter(productCode -> oldProductList.stream().noneMatch(map -> productCode.equals(map.get("sku")))).collect(Collectors.toList());
stream执行时间为:4019ms for循环执行时间为:3025ms
上网寻找了一下,单纯的使用流并不能有效的提升效率,所以将外部的流改成并行流(内外都用并行流效率反而会下降到1455ms,猜测是重复计算noneMatch导致)
List<String> addList = newList.parallelStream().filter(productCode -> oldProductList.stream().noneMatch(map -> productCode.equals(map.get("sku")))).collect(Collectors.toList());
stream执行时间为:957.0ms for循环执行时间为:2988.0ms 时间缩短为1/4!
然后又出来了一个疑惑,stream().parallel() 和.parallelStream()有什么区别(百度半天都是stream和parallelStream,代码一层一层下去又麻烦 干脆改改代码看时间) 顺序尝试三次,第三次时间明显大幅度减少,应该是缓存原因。
.parallelStream()执行时间为:998.0ms .stream().parallel()执行时间为:1145.0ms
.parallelStream()执行时间为:810.0ms .stream().parallel()执行时间为:1051.0ms
.parallelStream()执行时间为:489.0ms .stream().parallel()执行时间为:790.0ms