JavaGuide icon indicating copy to clipboard operation
JavaGuide copied to clipboard

容器篇中对于HashMap遍历方式的说明存在部分错误

Open houtaroy opened this issue 3 years ago • 0 comments

1.5.8. HashMap 有哪几种常见的遍历方式?

此处引用公众号文章中, 对于parallelStream遍历方式的性能分析有误

先说结论, 存在阻塞时parallelStream性能最高, 非阻塞时parallelStream性能最低

当遍历不存在阻塞时, parallelStream的性能是最低的:

Benchmark               Mode  Cnt     Score      Error  Units
Test.entrySet           avgt    5   288.651 ±   10.536  ns/op
Test.keySet             avgt    5   584.594 ±   21.431  ns/op
Test.lambda             avgt    5   221.791 ±   10.198  ns/op
Test.parallelStream     avgt    5  6919.163 ± 1116.139  ns/op

加入阻塞代码Thread.sleep(10)后, parallelStream的性能才是最高的:

Benchmark               Mode  Cnt           Score          Error  Units
Test.entrySet           avgt    5  1554828440.000 ± 23657748.653  ns/op
Test.keySet             avgt    5  1550612500.000 ±  6474562.858  ns/op
Test.lambda             avgt    5  1551065180.000 ± 19164407.426  ns/op
Test.parallelStream     avgt    5   186345456.667 ±  3210435.590  ns/op

houtaroy avatar Sep 23 '21 00:09 houtaroy