为什么IntStream.range(0,100000).parallel().foreach花费比正常for循环更长的时间 [英] Why does IntStream.range(0, 100000).parallel().foreach take longer then normal for loop

查看:237
本文介绍了为什么IntStream.range(0,100000).parallel().foreach花费比正常for循环更长的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Java中的Streams和并行,我想知道为什么普通的for循环比在将项目添加到数组中花费的IntStream更少的时间.

I am just starting to learn about the Streams and parallel in Java and I was wondering why a normal for loop takes less time than IntStream paralleled at adding items to an array.

package parallel;

import java.util.stream.IntStream;

public class Parallel {

    public static void main(String[] args) {
         final int[] intArray = new int[100000];
        long startTime = System.currentTimeMillis(); 
        IntStream.range(0, 100000).parallel().forEach(i ->  intArray[i]=i);
        long endTime = System.currentTimeMillis();
        System.out.println("Parallel time: " + (endTime-startTime));
        final int[] intArray2 = new int[100000];
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        startTime = System.currentTimeMillis();
        for(int i = 0; i < 100000; i++){
            intArray2[i] = i;
        }
        endTime = System.currentTimeMillis();
        System.out.println("Non parallel time: " + (endTime-startTime));
    }
}

获得这样的结果.

平行时间:110

非并行时间:7

推荐答案

您对每个元素执行的操作非常简单,这只是一个分配,非常快.在并行版本中,启动多个处理操作的线程会产生大量开销.仅此一项操作所花的时间可能要比非并行应用时进行非常简单的操作所花费的时间长.

The operation you perform for each element is very simple, it's just an assignment, which is very fast. In the parallel version, you have a lot of overhead by starting the multiple threads that handle the operations. This alone will likely already take longer than what the very simple operation takes when applied non-parallel.

此外,在非并行版本中,值以非常线性的方式写入数组,该CPU体系结构已经对单威胁/单核优化进行了相当长的一段时间,编译器和中间编译器也是如此(它们将代码转换为C进行组装).但是在并行版本中,当每个线程尝试写入同一数组时(尽管位置不同,但可能仍在同一缓存行上),当多个线程访问数组的不同部分时,您可能会遇到冲突.可能还会导致缓存未命中,这会使事情变慢.

Also, in the non-parallel version, the values are written very linearly to the array, which CPU architecture already has single-threat/single-core optimizations for quite awhile, as do compilers and intermediary compilers (which convert code like C to assembly). In the parallel version though, you'll might get conflicts as each thread tries to write to the same array (although on different positions, but probably still on the same cache line), and as several threads access different parts of the array, you might also get cache misses which slow things down.

使用更昂贵的操作,与总成本相比,并行版本的开销变得更小,最终将比非并行情况更快地执行.

With a more expensive operation, the overhead of the parallel version becomes smaller compared to the total costs, which will in the end result in faster execution than the non-parallel case.

这篇关于为什么IntStream.range(0,100000).parallel().foreach花费比正常for循环更长的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆