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
测试代码如下:
1 public class ForTest { 2 public static void main(String[] args) { 3 for( int t=1;t<5;t++){ 4 for(int tt=1;tt<=4;tt++){ 5 List<Integer> testList = Arrays.asList(new Integer[(int) Math.pow(100,(t))]); 6 long t1 = System.currentTimeMillis(); 7 for(int i=0;i<testList.size();i++){ 8 Integer b = testList.get(i); 9 } 10 long t2 = System.currentTimeMillis(); 11 12 for(Integer i:testList){ 13 Integer b = i; 14 } 15 long t3 = System.currentTimeMillis(); 16 17 18 testList.forEach(integer -> {Integer b = integer;}); 19 long t4 = System.currentTimeMillis(); 20 21 22 testList.stream().forEach(integer -> {Integer b = integer;}); 23 long t5 = System.currentTimeMillis(); 24 25 26 testList.parallelStream().forEach(integer -> {Integer b = integer;}); 27 long t6 = System.currentTimeMillis(); 28 29 Integer b; 30 for(Iterator<Integer> iterator = testList.iterator(); iterator.hasNext(); b = iterator.next()); 31 long t7 = System.currentTimeMillis(); 32 33 34 System.out.println("x" + tt +": loop size:" + testList.size()); 35 System.out.println("y" + t + ": classical for loop waste millisecond:"+(t2-t1)); 36 System.out.println("y" + t + ": classical forEach loop waste millisecond:"+(t3-t2)); 37 System.out.println("y" + t + ": lambda forEach loop waste millisecond:"+(t4-t3)); 38 System.out.println("y" + t + ": lambda not parallel stream forEach loop waste millisecond:"+(t5-t4)); 39 System.out.println("y" + t + ": lambda parallel stream forEach loop waste millisecond:"+(t6-t5)); 40 System.out.println("y" + t + ": classical iterator loop waste millisecond:"+(t7-t6)+"\n"); 41 } 42 } 43 } 44 }
测试结果:
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