我将比较两个Java8流终端操作
reduce()
和collect()
的并行性能。以下是一个Java8并行流示例:import java.math.BigInteger;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import static java.math.BigInteger.ONE;
public class StartMe {
static Function<Long, BigInteger> fac;
static {
fac = x -> x==0? ONE : BigInteger.valueOf(x).multiply(fac.apply(x - 1));
}
static long N = 2000;
static Supplier<BigInteger[]> one() {
BigInteger[] result = new BigInteger[1];
result[0] = ONE;
return () -> result;
}
static BiConsumer<BigInteger[], ? super BigInteger> accumulator() {
return (BigInteger[] ba, BigInteger b) -> {
synchronized (fac) {
ba[0] = ba[0].multiply(b);
}
};
}
static BiConsumer<BigInteger[], BigInteger[]> combiner() {
return (BigInteger[] b1, BigInteger[] b2) -> {};
}
public static void main(String[] args) throws Exception {
long t0 = System.currentTimeMillis();
BigInteger result1 = Stream.iterate(ONE, x -> x.add(ONE)).parallel().limit(N).reduce(ONE, BigInteger::multiply);
long t1 = System.currentTimeMillis();
BigInteger[] result2 = Stream.iterate(ONE, x -> x.add(ONE)).parallel().limit(N).collect(one(), accumulator(), combiner());
long t2 = System.currentTimeMillis();
BigInteger result3 = fac.apply(N);
long t3 = System.currentTimeMillis();
System.out.println("reduce(): deltaT = " + (t1-t0) + "ms, result 1 = " + result1);
System.out.println("collect(): deltaT = " + (t2-t1) + "ms, result 2 = " + result2[0]);
System.out.println("recursive: deltaT = " + (t3-t2) + "ms, result 3 = " + result3);
}
}
它使用一些算法(尽管可能有点奇怪)来计算n!。
然而,性能结果令人惊讶:
reduce(): deltaT = 44ms, result 1 = 3316275...
collect(): deltaT = 22ms, result 2 = 3316275...
recursive: deltaT = 11ms, result 3 = 3316275...
一些备注:
- 我不得不同步
accumulator()
,因为它并行访问同一数组。 - 我原本以为
reduce()
和collect()
的性能应该是一样的,但事实上reduce()
比collect()
慢了约两倍,尽管collect()
必须被同步! - 最快的算法是顺序和递归的算法(这可能会显示出并行流管理的巨大开销)
我没想到 reduce()
的性能会比 collect()
更差。为什么会这样?