在玩弄新的 Java 流时,我注意到与并行流的性能有关的一些奇怪的东西。我使用了一个简单的程序,它从文本文件中读取单词并计算长度大于 5 的单词(测试文件有 30000 个单词):
String contents = new String(Files.readAllBytes(Paths.get("text.txt")));
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));
long startTime;
for (int i = 0; i < 100; i++) {
startTime = System.nanoTime();
words.parallelStream().filter(w -> w.length() > 5).count();
System.out.println("Time elapsed [PAR]: " + (System.nanoTime() - startTime));
startTime = System.nanoTime();
words.stream().filter(w -> w.length() > 5).count();
System.out.println("Time elapsed [SEQ]: " + (System.nanoTime() - startTime));
System.out.println("------------------");
}
这会在我的机器上生成以下输出(我只提到第一次和最后 5 次循环迭代):
Time elapsed [PAR]: 114185196
Time elapsed [SEQ]: 3222664
------------------
Time elapsed [PAR]: 569611
Time elapsed [SEQ]: 797113
------------------
Time elapsed [PAR]: 678231
Time elapsed [SEQ]: 414807
------------------
Time elapsed [PAR]: 755633
Time elapsed [SEQ]: 679085
------------------
Time elapsed [PAR]: 755633
Time elapsed [SEQ]: 393425
------------------
...
Time elapsed [PAR]: 90232
Time elapsed [SEQ]: 163785
------------------
Time elapsed [PAR]: 80396
Time elapsed [SEQ]: 154805
------------------
Time elapsed [PAR]: 83817
Time elapsed [SEQ]: 154377
------------------
Time elapsed [PAR]: 81679
Time elapsed [SEQ]: 186449
------------------
Time elapsed [PAR]: 68849
Time elapsed [SEQ]: 154804
------------------
为什么第一个处理比其他处理慢 100 倍?为什么并行流在第一次迭代中比顺序流慢,但在最后一次迭代中快两倍?为什么顺序流和并行流都会随着时间的推移变得更快?这与循环优化有关吗?
稍后编辑:在 Luigi 的建议下,我使用JUnitBenchmarks实现了基准测试:
List<String> words = null;
@Before
public void setup() {
try {
String contents = new String(Files.readAllBytes(Paths.get("text.txt")));
words = Arrays.asList(contents.split("[\\P{L}]+"));
} catch (IOException e) {
e.printStackTrace();
}
}
@BenchmarkOptions(benchmarkRounds = 100)
@Test
public void parallelTest() {
words.parallelStream().filter(w -> w.length() > 5).count();
}
@BenchmarkOptions(benchmarkRounds = 100)
@Test
public void sequentialTest() {
words.stream().filter(w -> w.length() > 5).count();
}
我还将测试文件中的字数增加到 300000。新的结果是:
Benchmark.sequentialTest:[测量 105 轮中的 100 轮,线程:1(顺序)]
round:0.08 [+- 0.04],round.block:0.00 [+- 0.00],round.gc:0.00 [+- 0.00],GC.calls:62,GC.time:1.53,time.total:8.65,时间.warmup:0.81,time.bench:7.85
Benchmark.parallelTest:[测量 105 轮中的 100 轮,线程:1(顺序)]
round:0.06 [+- 0.02],round.block:0.00 [+- 0.00],round.gc:0.00 [+- 0.00],GC.calls:32,GC.time:0.79,time.total:6.82,时间.warmup:0.39,time.bench:6.43
所以看起来最初的结果是由错误的微基准配置引起的......