java8 for ,forEach ,lambda forEach , strean forEach , parller stream forEach, Iterator性能对比
java8 for ,forEach ,Iterator,lambda forEach ,lambda strean forEach , lambda parller stream forEach性能对比
性能结果如下:
lambda parallelStream().forEach() > lambda stream().forEach() ≈ lambda forEach() > classical iterator ≈ classical forEach > classical for
测试代码如下:
public class ForTest {
public static void main(String[] args) {
for( int t=1;t<5;t++){
for(int tt=1;tt<=4;tt++){
List<Integer> testList = Arrays.asList(new Integer[(int) Math.pow(100,(t))]);
long t1 = System.currentTimeMillis();
for(int i=0;i<testList.size();i++){
Integer b = testList.get(i);
}
long t2 = System.currentTimeMillis(); for(Integer i:testList){
Integer b = i;
}
long t3 = System.currentTimeMillis(); testList.forEach(integer -> {Integer b = integer;});
long t4 = System.currentTimeMillis(); testList.stream().forEach(integer -> {Integer b = integer;});
long t5 = System.currentTimeMillis(); testList.parallelStream().forEach(integer -> {Integer b = integer;});
long t6 = System.currentTimeMillis(); Integer b;
for(Iterator<Integer> iterator = testList.iterator(); iterator.hasNext(); b = iterator.next());
long t7 = System.currentTimeMillis(); System.out.println("x" + tt +": loop size:" + testList.size());
System.out.println("y" + t + ": classical for loop waste millisecond:"+(t2-t1));
System.out.println("y" + t + ": classical forEach loop waste millisecond:"+(t3-t2));
System.out.println("y" + t + ": lambda forEach loop waste millisecond:"+(t4-t3));
System.out.println("y" + t + ": lambda not parallel stream forEach loop waste millisecond:"+(t5-t4));
System.out.println("y" + t + ": lambda parallel stream forEach loop waste millisecond:"+(t6-t5));
System.out.println("y" + t + ": classical iterator loop waste millisecond:"+(t7-t6)+"\n");
}
}
}
}
测试结果:
x1: loop size:100
y1: classical for loop waste millisecond:0
y1: classical forEach loop waste millisecond:0
y1: lambda forEach loop waste millisecond:74
y1: lambda not parallel stream forEach loop waste millisecond:2
y1: lambda parallel stream forEach loop waste millisecond:7
y1: classical iterator loop waste millisecond:0 x2: loop size:100
y1: classical for loop waste millisecond:0
y1: classical forEach loop waste millisecond:0
y1: lambda forEach loop waste millisecond:1
y1: lambda not parallel stream forEach loop waste millisecond:0
y1: lambda parallel stream forEach loop waste millisecond:0
y1: classical iterator loop waste millisecond:0 x3: loop size:100
y1: classical for loop waste millisecond:1
y1: classical forEach loop waste millisecond:0
y1: lambda forEach loop waste millisecond:0
y1: lambda not parallel stream forEach loop waste millisecond:1
y1: lambda parallel stream forEach loop waste millisecond:0
y1: classical iterator loop waste millisecond:0 x4: loop size:100
y1: classical for loop waste millisecond:0
y1: classical forEach loop waste millisecond:0
y1: lambda forEach loop waste millisecond:0
y1: lambda not parallel stream forEach loop waste millisecond:0
y1: lambda parallel stream forEach loop waste millisecond:0
y1: classical iterator loop waste millisecond:0 x1: loop size:10000
y2: classical for loop waste millisecond:1
y2: classical forEach loop waste millisecond:0
y2: lambda forEach loop waste millisecond:1
y2: lambda not parallel stream forEach loop waste millisecond:0
y2: lambda parallel stream forEach loop waste millisecond:1
y2: classical iterator loop waste millisecond:1 x2: loop size:10000
y2: classical for loop waste millisecond:1
y2: classical forEach loop waste millisecond:1
y2: lambda forEach loop waste millisecond:0
y2: lambda not parallel stream forEach loop waste millisecond:0
y2: lambda parallel stream forEach loop waste millisecond:1
y2: classical iterator loop waste millisecond:1 x3: loop size:10000
y2: classical for loop waste millisecond:0
y2: classical forEach loop waste millisecond:1
y2: lambda forEach loop waste millisecond:0
y2: lambda not parallel stream forEach loop waste millisecond:0
y2: lambda parallel stream forEach loop waste millisecond:1
y2: classical iterator loop waste millisecond:0 x4: loop size:10000
y2: classical for loop waste millisecond:1
y2: classical forEach loop waste millisecond:0
y2: lambda forEach loop waste millisecond:0
y2: lambda not parallel stream forEach loop waste millisecond:0
y2: lambda parallel stream forEach loop waste millisecond:1
y2: classical iterator loop waste millisecond:0 x1: loop size:1000000
y3: classical for loop waste millisecond:14
y3: classical forEach loop waste millisecond:9
y3: lambda forEach loop waste millisecond:6
y3: lambda not parallel stream forEach loop waste millisecond:8
y3: lambda parallel stream forEach loop waste millisecond:3
y3: classical iterator loop waste millisecond:7 x2: loop size:1000000
y3: classical for loop waste millisecond:17
y3: classical forEach loop waste millisecond:12
y3: lambda forEach loop waste millisecond:7
y3: lambda not parallel stream forEach loop waste millisecond:0
y3: lambda parallel stream forEach loop waste millisecond:1
y3: classical iterator loop waste millisecond:18 x3: loop size:1000000
y3: classical for loop waste millisecond:14
y3: classical forEach loop waste millisecond:19
y3: lambda forEach loop waste millisecond:0
y3: lambda not parallel stream forEach loop waste millisecond:1
y3: lambda parallel stream forEach loop waste millisecond:0
y3: classical iterator loop waste millisecond:17 x4: loop size:1000000
y3: classical for loop waste millisecond:13
y3: classical forEach loop waste millisecond:25
y3: lambda forEach loop waste millisecond:1
y3: lambda not parallel stream forEach loop waste millisecond:0
y3: lambda parallel stream forEach loop waste millisecond:1
y3: classical iterator loop waste millisecond:31 x1: loop size:100000000
y4: classical for loop waste millisecond:237
y4: classical forEach loop waste millisecond:107
y4: lambda forEach loop waste millisecond:73
y4: lambda not parallel stream forEach loop waste millisecond:70
y4: lambda parallel stream forEach loop waste millisecond:28
y4: classical iterator loop waste millisecond:98 x2: loop size:100000000
y4: classical for loop waste millisecond:158
y4: classical forEach loop waste millisecond:92
y4: lambda forEach loop waste millisecond:59
y4: lambda not parallel stream forEach loop waste millisecond:54
y4: lambda parallel stream forEach loop waste millisecond:26
y4: classical iterator loop waste millisecond:66 x3: loop size:100000000
y4: classical for loop waste millisecond:123
y4: classical forEach loop waste millisecond:67
y4: lambda forEach loop waste millisecond:54
y4: lambda not parallel stream forEach loop waste millisecond:52
y4: lambda parallel stream forEach loop waste millisecond:21
y4: classical iterator loop waste millisecond:70 x4: loop size:100000000
y4: classical for loop waste millisecond:201
y4: classical forEach loop waste millisecond:92
y4: lambda forEach loop waste millisecond:54
y4: lambda not parallel stream forEach loop waste millisecond:53
y4: lambda parallel stream forEach loop waste millisecond:23
y4: classical iterator loop waste millisecond:67
java8 for ,forEach ,lambda forEach , strean forEach , parller stream forEach, Iterator性能对比的更多相关文章
- Java8特性之Lambda、方法引用以及Stream流
Java 8 中的 Streams API 详解:https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/ Java笔记——Jav ...
- Java8新特性之forEach+Lambda 表达式遍历Map和List
这是Java8系列的第二篇,今天来说一下Java8中forEach的简单使用.我们使用对比的方式来看应该会看得更加清楚,更能理解: 一.遍历Map ============Java8之前的方式==== ...
- java8:(Lambda 表达式,Supplier,@FunctionalInterface,foreach(),Optional,Stream().collect,双冒号,joining,partitioningBy分区,collectingAndThen,filter())
1.Lambda 表达式: 引导:http://www.cnblogs.com/yulinfeng/p/8452379.html DEMO1: List<String> names1 = ...
- Java8 关于stream.foreach()和stream.peek()的区别解析
该思考来源于日常工作中,特记此心得. 思考:如何快速将list中的每个item内部属性值改变并进行其他流体操作呢? 下面做个测试:如何先在list中统一改变某属性的值,然后再根据某个属性取出该属性值最 ...
- 面试:Stream#foreach方法摸底三问,你都了解吗
JAVA8 新增了 Stream API,而在 Stream API 中又为程序员提供了一个遍历集合的 foreach 方法:java.util.stream.Stream#forEach. 那你对这 ...
- The 'stream().forEach()' chain can be replaced with 'forEach()' (may change semantics)
对集合操作时,因不同的写法Idea经常会提示:The 'stream().forEach()' chain can be replaced with 'forEach()' (may change s ...
- Java笔记——Java8特性之Lambda、方法引用和Streams
Java8已经推出了好一段时间了,而掌握Java8的新特性也是必要的,如果要进行Spring开发,那么可以发现Spring的官网已经全部使用Java8来编写示例代码了,所以,不学就看不懂. 这里涉及三 ...
- Java8学习笔记----Lambda表达式 (转)
Java8学习笔记----Lambda表达式 天锦 2014-03-24 16:43:30 发表于:ATA之家 本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人 ...
- Java8一:Lambda表达式教程
1. 什么是λ表达式 λ表达式本质上是一个匿名方法.让我们来看下面这个例子: public int add(int x, int y) { return x + y; } 转成 ...
随机推荐
- PY序
Python实现机器学习依赖于两个类库——SciPy和scikit-learn 一)SciPy SciPy是数学运算的基本类库,在机器学习的过程中,主要运用NumPy.Matplotlib和Panda ...
- maven依赖冲突
https://blog.csdn.net/noaman_wgs/article/details/81137893
- js中this最简单清晰的解释
引自 https://www.cnblogs.com/huangwentian/p/6854472.html#commentform ① this指向的,永远只可能是对象! ② this ...
- netty拆包粘包
客户端 tcp udp socket网络编程接口 http/webservice mqtt/xmpp 自定义RPC (dubbo) 应用层 服务端 ServerSocket ss = new serv ...
- JDK安装教程
打开我的电脑,在D盘中新建一个文件夹,名字为develop 进入develop,创建一个新文件夹,名字叫做jdk 双击JDK的安装包, .4.出如图所示的框,选择下一步 .5.更改安装路径,选择更改 ...
- python3-基础8
模块与包 什么是模块 模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. #在python中,模块的使用方式都是一样的,但其实细说的话,模块可以分为四个通用类别: 1 ...
- 利用grep参数查看某关键词前后几行内容
查看文件中含有“哈哈哈”关键字所在行后5行内容 cat xxxxxx | grep -A 5 哈哈哈 查看文件中含有“哈哈哈”关键字所在行前5行内容 cat xxxxxx | grep -B 5 哈哈 ...
- JAVA的DES加密解密在windows上测试一切正常,在linux上异常
windows上加解密正常,linux上加密正常,解密时发生 如下异常,异常信息如下: [ERROR] 2018-10-15 09:30:35,998 method:com.iscas.ippc.co ...
- hive 中间会话临时文件自动清理脚本
hive在运行过程中,我们经常会发现 hdfs /tmp/hive/hive or hdfs 目录占用空间巨大,我这里已经清理过了所以,相对较小.但是初次使用hive的用户,一定会发现 随着时间的推移 ...
- WinForm-简单21点纸牌小游戏
纸牌游戏有很多种玩法,C#代码写的纸牌游戏,网上也能找到不少,从中也能学习到不少知识,自己动手也写一个,算是记录下学习过程吧. 纸牌21点的玩法也比较简单,就是看谁手中的所有牌相加是21点,或是离21 ...