一、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的并行计算的更多相关文章

  1. java8Stream原理深度解析

    Java8 Stream原理深度解析 Author:Dorae Date:2017年11月2日19:10:39 转载请注明出处 上一篇文章中简要介绍了Java8的函数式编程,而在Java8中另外一个比 ...

  2. SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)

    前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...

  3. Java 使用 Stream API 筛选 List

    前言 上课的时候看到老师用迭代器来遍历 List 中的元素的时候,我的内心是极其嫌弃的,这种迭代方法不能直接访问当前的元素,而且写起来也麻烦.于是上网查了查 Java 有没有类似于 Linq 的东西, ...

  4. Java Stream 使用详解

    Stream是 Java 8新增加的类,用来补充集合类. Stream代表数据流,流中的数据元素的数量可能是有限的,也可能是无限的. Stream和其它集合类的区别在于:其它集合类主要关注与有限数量的 ...

  5. baike并行计算概念

    并行计算 概论 ▪ 高性能计算 ▪ 计算机集群 ▪ 分布式计算 ▪ 网格计算 ▪ 云端运算         方式 ▪ Bit-level parallelism ▪ Instruction level ...

  6. [Java 8 Lambda] java.util.stream 简单介绍

    包结构例如以下所看到的: 这个包的结构非常easy,类型也不多. BaseStream接口 全部Stream接口类型的父接口,它继承自AutoClosable接口,定义了一些全部Stream都具备的行 ...

  7. Java8 Stream简介

    Stream是Java 8新增的重要特性, 它提供函数式编程支持并允许以管道方式操作集合. 流操作会遍历数据源, 使用管道式操作处理数据后生成结果集合, 这个过程通常不会对数据源造成影响. lambd ...

  8. [三]java8 函数式编程Stream 概念深入理解 Stream 运行原理 Stream设计思路

    Stream的概念定义   官方文档是永远的圣经~     表格内容来自https://docs.oracle.com/javase/8/docs/api/   Package java.util.s ...

  9. [零]java8 函数式编程入门官方文档中文版 java.util.stream 中文版 流处理的相关概念

    前言 本文为java.util.stream 包文档的译文 极其个别部分可能为了更好理解,陈述略有改动,与原文几乎一致 原文可参考在线API文档 https://docs.oracle.com/jav ...

随机推荐

  1. 动态class,style,src绑定写法 vue

    :class="{active:menuName===item.title}" :style="thisTitle==='一张图展示'?styles:''" : ...

  2. redis、rabitmq对比

    redis.rabitmq对比 原文地址 简要介绍 RabbitMQ RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性 ...

  3. Windows 下apache https配置(phpstudy)

    1.首先获取证书,https://www.pianyissl.com/  免费三个月的 或者 自己生成私钥.证书,然后应用到apache中. http://blog.sina.com.cn/s/blo ...

  4. ssh远程登录故障解决方案

    问题描述: xshell远程连接服务器连接不上,如下图所示: 故障排除: . 首先查看自己系统的防火墙是否关闭,没有关闭的话关闭一下. # centos 7中关闭防火墙命令: systemctl st ...

  5. pycharm使用已经配置好的virtualenv环境

    前面已经介绍过基本的virtualenv使用方法,如想要了解的同学可以跳转https://www.cnblogs.com/weilijie/p/10964872.html 下面进入正题,我使用的是py ...

  6. linux开启Rsyslog服务收集日志

    一.查看是否安装了rsyslog服务 [root@server- ~]# yum install -y rsyslog 已加载插件:fastestmirror Loading mirror speed ...

  7. 转载:网络编程 socket 可读可写条件判断

    转自:http://blog.csdn.net/majianfei1023/article/details/45788591 要了解socket可读可写条件,我们先了解几个概念:1.接收缓存区低水位标 ...

  8. DECLARE_GLOBAL_DATA_PTR

    DECLARE_GLOBAL_DATA_PTR在arch/arm/include/asm/global_data.h中定义 #include <asm-generic/global_data.h ...

  9. 2019强网杯web upload分析(pop链)

    参考链接:https://blog.csdn.net/qq_41173457/article/details/90724943 注意 只要namespace相同那就可以直接实例化同一namespace ...

  10. npm 全局安装和局部安装的区别

    上图是从网上找的webpack 安装的步骤,我们看到除了要全局安装之外,还需要本地安装,那么这两者有什么区别呢? 本文以Windows平台上做测试,以webpack为示例做教程 什么是全局安装? 安装 ...