for循环和IntStream.forEach()性能比较

1.测试代码

import java.util.stream.IntStream;

/**
 * <p> @Title Test
 * <p> @Description 测试类
 *
 * @author ACGkaka
 * @date 2020/4/18 7:13
 */
public class Test {

    /**
     * 测试for()循环
     */
    private static void forTest() throws InterruptedException {
        for (int i = 0; i < 1_000; i++) {
            Thread.sleep(1);
        }
    }

    /**
     * 测试IntStream.forEach()
     */
    private static void streamTest() {
        IntStream.rangeClosed(0, 1_000).forEach(i -> {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 测试IntStream.parallel().forEach()
     */
    private static void parallelStreamTest() {
        IntStream.rangeClosed(0, 1_000).parallel().forEach(i -> {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    public static void main(String[] args) throws InterruptedException {
        long t1 = 0;
        long t2 = 0;
        long t3 = 0;
        for (int i = 0; i < 5; i++) {
            long start = System.nanoTime();
            forTest();
            long period = System.nanoTime() - start;
            System.out.println("forTest() \t\t\t\tspends \t" + period + "\tns");
            t1 += period;

            start = System.nanoTime();
            streamTest();
            period = System.nanoTime() - start;
            System.out.println("streamTest() \t\t\tspends \t" + period + "\tns");
            t2 += period;

            start = System.nanoTime();
            parallelStreamTest();
            period = System.nanoTime() - start;
            System.out.println("parallelStreamTest() \tspends \t" + period + "\tns");
            t3 += period;
        }
        System.out.println("forTest() \t\t\t\t总耗时: " + t1 + "\tns; 平均耗时: " + (t1 / 5));
        System.out.println("streamTest() \t\t\t总耗时: " + t2 + "\tns; 平均耗时: " + (t2 / 5));
        System.out.println("parallelStreamTest() \t总耗时: " + t2 + "\tns; 平均耗时: " + (t3 / 5));
    }
}

2.输出结果

forTest() 				spends 	1839452800	ns
streamTest() 			spends 	1728509300	ns
parallelStreamTest() 	spends 	223115100	ns
forTest() 				spends 	1725872700	ns
streamTest() 			spends 	1713570500	ns
parallelStreamTest() 	spends 	207338200	ns
forTest() 				spends 	1689249000	ns
streamTest() 			spends 	1714935500	ns
parallelStreamTest() 	spends 	214474700	ns
forTest() 				spends 	1678805400	ns
streamTest() 			spends 	1679608600	ns
parallelStreamTest() 	spends 	214144400	ns
forTest() 				spends 	1691222600	ns
streamTest() 			spends 	1701802500	ns
parallelStreamTest() 	spends 	211717200	ns
forTest() 				总耗时: 8624602500	ns; 平均耗时: 1724920500
streamTest() 			总耗时: 8538426400	ns; 平均耗时: 1707685280
parallelStreamTest() 	总耗时: 8538426400	ns; 平均耗时: 214157920

3.结论

for() 循环 和 IntStream.forEach() 循环 没有性能差距。但是如果IntStream后面加上parallel()的并行流操作,可以根据CPU的内核数量成倍地提升速度。我的CPU是8核的,所以IntStream.parallel().forEach()的运行速度提升了8倍。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿放下技术的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值