package com.shrimpking.t4;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/9/12 12:09
*/
public interface Test
{
void test();
}
package com.shrimpking.t4;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/9/12 12:10
*/
public class Tester
{
public static long runTest(Test t){
long start = System.currentTimeMillis();
t.test();
long end = System.currentTimeMillis();
return end - start;
}
}
package com.shrimpking.t4;
import java.util.stream.LongStream;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/9/12 12:09
*/
public class ParallelStreamPerformance
{
public static void main(String[] args)
{
LongStream stream1 = LongStream.rangeClosed(1L,1000L);
// long time1 = Tester.runTest(new Test()
// {
// @Override
// public void test()
// {
// stream1.reduce(0L,Long::sum);
// }
// });
long time1 = Tester.runTest(() -> stream1.reduce(0L,Long::sum));
System.out.println("串行流花费时间:" + time1);
LongStream stream2 = LongStream.rangeClosed(1L,1000L).parallel();
long time2 = Tester.runTest(()-> stream2.reduce(0L,Long::sum));
System.out.println("并行流花费时间:" + time2);
LongStream stream3 = LongStream.rangeClosed(1L,100000000L);
long time3 = Tester.runTest(() -> stream3.reduce(0L,Long::sum));
System.out.println("数据量大串行流花费时间:" + time3);
LongStream stream4 = LongStream.rangeClosed(1L,100000000L).parallel();
long time4 = Tester.runTest(() -> stream4.reduce(0L,Long::sum));
System.out.println("数据量大并行流花费时间:" + time4);
//并行流的性能
//处理少量数据时,并行流与串行流差距不大
//处理大量数据时,并行流优势明显
}
}
标签:LongStream,Tester,ParallelStreamPerformance,System,long,runTest,public
From: https://blog.51cto.com/u_15356972/12016822