我是靠谱客的博主 孝顺信封,最近开发中收集的这篇文章主要介绍java各种List循环:for、forEach、lambda forEach、stream forEach、iterator性能效率对比,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
public static void main(String[] args) {
for( int m=1;m<5;m++){
for(int n=1;n<=4;n++){
List<Integer> testList = Arrays.asList(new Integer[(int) Math.pow(100,(m))]);
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" + n +": loop size:" + testList.size());
System.out.println("y" + m + ": classical for loop waste millisecond:"+(t2-t1));
System.out.println("y" + m + ": classical forEach loop waste millisecond:"+(t3-t2));
System.out.println("y" + m + ": lambda forEach loop waste millisecond:"+(t4-t3));
System.out.println("y" + m + ": lambda not parallel stream forEach loop waste millisecond:"+(t5-t4));
System.out.println("y" + m + ": lambda parallel stream forEach loop waste millisecond:"+(t6-t5));
System.out.println("y" + m + ": 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:245
y1: lambda not parallel stream forEach loop waste millisecond:5
y1: lambda parallel stream forEach loop waste millisecond:36
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: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
x3: 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
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:3
y1: classical iterator loop waste millisecond:0
x1: loop size:10000
y2: classical for loop waste millisecond:1
y2: classical forEach loop waste millisecond:10
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:3
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:1
y2: lambda parallel stream forEach loop waste millisecond:1
y2: classical iterator loop waste millisecond:2
x3: loop size:10000
y2: classical for loop waste millisecond:1
y2: classical forEach loop waste millisecond:2
y2: lambda forEach loop waste millisecond:3
y2: lambda not parallel stream forEach loop waste millisecond:0
y2: lambda parallel stream forEach loop waste millisecond:0
y2: classical iterator loop waste millisecond:1
x4: loop size:10000
y2: classical for loop waste millisecond:0
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:0
x1: loop size:1000000
y3: classical for loop waste millisecond:82
y3: classical forEach loop waste millisecond:194
y3: lambda forEach loop waste millisecond:11
y3: lambda not parallel stream forEach loop waste millisecond:11
y3: lambda parallel stream forEach loop waste millisecond:5
y3: classical iterator loop waste millisecond:33
x2: loop size:1000000
y3: classical for loop waste millisecond:20
y3: classical forEach loop waste millisecond:24
y3: lambda forEach loop waste millisecond:11
y3: lambda not parallel stream forEach loop waste millisecond:2
y3: lambda parallel stream forEach loop waste millisecond:3
y3: classical iterator loop waste millisecond:54
x3: loop size:1000000
y3: classical for loop waste millisecond:8
y3: classical forEach loop waste millisecond:2
y3: lambda forEach loop waste millisecond:2
y3: lambda not parallel stream forEach loop waste millisecond:2
y3: lambda parallel stream forEach loop waste millisecond:10
y3: classical iterator loop waste millisecond:95
x4: loop size:1000000
y3: classical for loop waste millisecond:16
y3: classical forEach loop waste millisecond:2
y3: lambda forEach loop waste millisecond:3
y3: lambda not parallel stream forEach loop waste millisecond:1
y3: lambda parallel stream forEach loop waste millisecond:50
y3: classical iterator loop waste millisecond:3
x1: loop size:100000000
y4: classical for loop waste millisecond:1563
y4: classical forEach loop waste millisecond:248
y4: lambda forEach loop waste millisecond:199
y4: lambda not parallel stream forEach loop waste millisecond:168
y4: lambda parallel stream forEach loop waste millisecond:64
y4: classical iterator loop waste millisecond:202
x2: loop size:100000000
y4: classical for loop waste millisecond:510
y4: classical forEach loop waste millisecond:223
y4: lambda forEach loop waste millisecond:159
y4: lambda not parallel stream forEach loop waste millisecond:151
y4: lambda parallel stream forEach loop waste millisecond:50
y4: classical iterator loop waste millisecond:205
x3: loop size:100000000
y4: classical for loop waste millisecond:521
y4: classical forEach loop waste millisecond:215
y4: lambda forEach loop waste millisecond:150
y4: lambda not parallel stream forEach loop waste millisecond:155
y4: lambda parallel stream forEach loop waste millisecond:46
y4: classical iterator loop waste millisecond:203
x4: loop size:100000000
y4: classical for loop waste millisecond:505
y4: classical forEach loop waste millisecond:214
y4: lambda forEach loop waste millisecond:149
y4: lambda not parallel stream forEach loop waste millisecond:149
y4: lambda parallel stream forEach loop waste millisecond:56
y4: classical iterator loop waste millisecond:205
最后
以上就是孝顺信封为你收集整理的java各种List循环:for、forEach、lambda forEach、stream forEach、iterator性能效率对比的全部内容,希望文章能够帮你解决java各种List循环:for、forEach、lambda forEach、stream forEach、iterator性能效率对比所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复