Stream的并行计算
一、Stream并行计算体验,利用多核加快计算速度
stream的并发,多个cpu执行同一个任务,提高效率;
需求:从1+...+10000000,看下各种计算方法的运行时间是多少
代码例子如下:
package com.cy.java8; import java.util.function.Function;
import java.util.stream.LongStream;
import java.util.stream.Stream; public class ParallelProcessing { public static void main(String[] args) {
//查看计算机核心线程数
//System.out.println(Runtime.getRuntime().availableProcessors()); long fastest1 = measureSumPerformance(ParallelProcessing::normalAdd, 10000000);
System.out.println("normalAdd the best processing time : " + fastest1 + " ms"); long fastest2 = measureSumPerformance(ParallelProcessing::iterateStream, 10000000);
System.out.println("iterateStream the best processing time : " + fastest2 + " ms"); long fastest3 = measureSumPerformance(ParallelProcessing::parallelStream, 10000000);
System.out.println("parallelStream the best processing time : " + fastest3 + " ms"); long fastest4 = measureSumPerformance(ParallelProcessing::parallelStream2, 10000000);
System.out.println("parallelStream2 the best processing time : " + fastest4 + " ms"); long fastest5 = measureSumPerformance(ParallelProcessing::parallelStream3, 10000000);
System.out.println("parallelStream3 the best processing time : " + fastest5 + " ms"); } /**
* 将下面的求和方法分别计算10次,取10次中运行最短的时间
*/
private static long measureSumPerformance(Function<Long, Long> adder, long limit){
long fastest = Long.MAX_VALUE; for(int i=0; i<10; i++){
long startTime = System.currentTimeMillis();
long result = adder.apply(limit);
long spendTime = System.currentTimeMillis() - startTime;
System.out.println("the sum result is " + result);
if(spendTime < fastest){
fastest = spendTime;
}
}
return fastest;
} /**
* 计算一串long类型的总和,普通的stream
* @param limit
* @return
*/
private static long iterateStream(long limit){
return Stream.iterate(1L, i->i+1).limit(limit).reduce(0L, Long::sum);
} /**
* 使用Stream.parallel
* 比较慢,为什么?
* Stream.iterate不适合并行计算
*/
private static long parallelStream(long limit){
return Stream.iterate(1L, i->i+1).parallel().limit(limit).reduce(0L, Long::sum);
} /**
* 将上面的Stream先自动拆箱为long,再并行
* 虽然拆箱为LongStream,还是很慢,为什么?
* Stream.iterate不适合并行计算
*/
private static long parallelStream2(long limit){
return Stream.iterate(1L, i -> i + 1)
.mapToLong(Long::longValue)
.parallel().limit(limit).reduce(0L, Long::sum);
} /**
* 使用LongStream.range
* 很快,比normalAdd快了近一倍,为什么?
* LongStream、IntStream等..它们的IntStream.range非常卓越的适合并行计算
*/
private static long parallelStream3(long limit){
return LongStream.rangeClosed(1, limit).parallel().reduce(0L, Long::sum);
} /**
* 以前的写法
* @param limit
* @return
*/
private static long normalAdd(long limit){
long result = 0L;
for(long i=0L; i <= limit; i++){
result += i;
}
return result;
}
}
console:
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
normalAdd the best processing time : 3 ms
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
iterateStream the best processing time : 78 ms
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
parallelStream the best processing time : 128 ms
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
parallelStream2 the best processing time : 178 ms
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
the sum result is 50000005000000
parallelStream3 the best processing time : 0 ms
结论:不一定是所有的方法产生的Stream都适合于并行的方式去做的,一定要注意有些方法是对于并行是厌恶的,有些方法是喜欢并行的;
列举一些例子如下:
数据源 分解性能
Source Decomposability
ArrayList Excellent
LinkedList Poor
IntStream.range Excellent
Stream.iterate Poor
HashSet Good
TreeSet Good
----
Stream的并行计算的更多相关文章
- java8Stream原理深度解析
Java8 Stream原理深度解析 Author:Dorae Date:2017年11月2日19:10:39 转载请注明出处 上一篇文章中简要介绍了Java8的函数式编程,而在Java8中另外一个比 ...
- SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)
前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...
- Java 使用 Stream API 筛选 List
前言 上课的时候看到老师用迭代器来遍历 List 中的元素的时候,我的内心是极其嫌弃的,这种迭代方法不能直接访问当前的元素,而且写起来也麻烦.于是上网查了查 Java 有没有类似于 Linq 的东西, ...
- Java Stream 使用详解
Stream是 Java 8新增加的类,用来补充集合类. Stream代表数据流,流中的数据元素的数量可能是有限的,也可能是无限的. Stream和其它集合类的区别在于:其它集合类主要关注与有限数量的 ...
- baike并行计算概念
并行计算 概论 ▪ 高性能计算 ▪ 计算机集群 ▪ 分布式计算 ▪ 网格计算 ▪ 云端运算 方式 ▪ Bit-level parallelism ▪ Instruction level ...
- [Java 8 Lambda] java.util.stream 简单介绍
包结构例如以下所看到的: 这个包的结构非常easy,类型也不多. BaseStream接口 全部Stream接口类型的父接口,它继承自AutoClosable接口,定义了一些全部Stream都具备的行 ...
- Java8 Stream简介
Stream是Java 8新增的重要特性, 它提供函数式编程支持并允许以管道方式操作集合. 流操作会遍历数据源, 使用管道式操作处理数据后生成结果集合, 这个过程通常不会对数据源造成影响. lambd ...
- [三]java8 函数式编程Stream 概念深入理解 Stream 运行原理 Stream设计思路
Stream的概念定义 官方文档是永远的圣经~ 表格内容来自https://docs.oracle.com/javase/8/docs/api/ Package java.util.s ...
- [零]java8 函数式编程入门官方文档中文版 java.util.stream 中文版 流处理的相关概念
前言 本文为java.util.stream 包文档的译文 极其个别部分可能为了更好理解,陈述略有改动,与原文几乎一致 原文可参考在线API文档 https://docs.oracle.com/jav ...
随机推荐
- BTE增强解析
原理:转载http://blog.csdn.net/wbin9752/article/details/7954663 BTEs(Business Transaction Events),是SAP的一种 ...
- 扫描全能王 v5.13.0.20190916 去水印和广告版
说明 1.先安装1(安装完不要打开),再安装2,然后打开2,参考下图: 2.不要登录扫描全能王账号,否则会导致失败! 3.激活完成后可以卸载2 下载地址 城通网盘 蓝奏云(仅含1) 百度网盘 另外口袋 ...
- ipc - System V 进程间通信机制
SYNOPSIS 总览 # include <sys/types.h> # include <sys/ipc.h> # include <sys/msg.h> # ...
- cacti监控
cacti监控 cacti简介 Cacti是一套基于php,mysql,snmp及rrdtool开发的网络流量监测图形分析工具.它通过snmpget获取数据,使用rrdtool绘画图形 Cacti轮询 ...
- Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard(思维,构造)
E. Connected Component on a Chessboard time limit per test2 seconds memory limit per test256 megabyt ...
- Java内部类的整理。
Java 内部类 分四种:成员内部类.局部内部类.静态内部类和匿名内部类. 成员内部类: (1)即作为外部类的一个成员存在,与外部类的属性.方法并列. 注意:成员内部类中不能定义静态变量,但可以访问外 ...
- 清楚webView的缓存
+(void)clearCache{ NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage s ...
- 负载均衡(四)Nginx负载均衡策略
一.Nginx的作用 1.反向代理 代理:转发请求的服务器,分代理和反向代理.代理一般指的是我们使用的DNS,反向代理是放在服务端的大家通常用Nginx来解决.实际应用中,由于服务端处于一个中心位置, ...
- curl检查访问网页返回的状态码
urls=('www.baidu.com' 'mm.yellowurl.cn' 'm.yellowurl.cn' 'http://m.yellowurl.cn/product/a.html'); fo ...
- Maven:mirror和repository
1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository ...