gpt4 book ai didi

java - Stream.toList() 会比 Collectors.toList() 表现得更好吗

转载 作者:行者123 更新时间:2023-12-03 11:17:38 25 4
gpt4 key购买 nike

JDK 引入了一个 API Stream.toList() JDK-8180352 。这是我尝试将其性能与现有 Collectors.toList 进行比较的基准测试代码:

@BenchmarkMode(Mode.All)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 20, time = 1, batchSize = 10000)
@Measurement(iterations = 20, time = 1, batchSize = 10000)
public class CollectorsVsStreamToList {

@Benchmark
public List<Integer> viaCollectors() {
return IntStream.range(1, 1000).boxed().collect(Collectors.toList());
}

@Benchmark
public List<Integer> viaStream() {
return IntStream.range(1, 1000).boxed().toList();
}
}
结果总结如下:
Benchmark                                                       Mode  Cnt   Score    Error  Units
CollectorsVsStreamToList.viaCollectors thrpt 20 17.321 ± 0.583 ops/s
CollectorsVsStreamToList.viaStream thrpt 20 23.879 ± 1.682 ops/s
CollectorsVsStreamToList.viaCollectors avgt 20 0.057 ± 0.002 s/op
CollectorsVsStreamToList.viaStream avgt 20 0.040 ± 0.001 s/op
CollectorsVsStreamToList.viaCollectors sample 380 0.054 ± 0.001 s/op
CollectorsVsStreamToList.viaCollectors:viaCollectors·p0.00 sample 0.051 s/op
CollectorsVsStreamToList.viaCollectors:viaCollectors·p0.50 sample 0.054 s/op
CollectorsVsStreamToList.viaCollectors:viaCollectors·p0.90 sample 0.058 s/op
CollectorsVsStreamToList.viaCollectors:viaCollectors·p0.95 sample 0.058 s/op
CollectorsVsStreamToList.viaCollectors:viaCollectors·p0.99 sample 0.062 s/op
CollectorsVsStreamToList.viaCollectors:viaCollectors·p0.999 sample 0.068 s/op
CollectorsVsStreamToList.viaCollectors:viaCollectors·p0.9999 sample 0.068 s/op
CollectorsVsStreamToList.viaCollectors:viaCollectors·p1.00 sample 0.068 s/op
CollectorsVsStreamToList.viaStream sample 525 0.039 ± 0.001 s/op
CollectorsVsStreamToList.viaStream:viaStream·p0.00 sample 0.037 s/op
CollectorsVsStreamToList.viaStream:viaStream·p0.50 sample 0.038 s/op
CollectorsVsStreamToList.viaStream:viaStream·p0.90 sample 0.040 s/op
CollectorsVsStreamToList.viaStream:viaStream·p0.95 sample 0.042 s/op
CollectorsVsStreamToList.viaStream:viaStream·p0.99 sample 0.050 s/op
CollectorsVsStreamToList.viaStream:viaStream·p0.999 sample 0.051 s/op
CollectorsVsStreamToList.viaStream:viaStream·p0.9999 sample 0.051 s/op
CollectorsVsStreamToList.viaStream:viaStream·p1.00 sample 0.051 s/op
CollectorsVsStreamToList.viaCollectors ss 20 0.060 ± 0.007 s/op
CollectorsVsStreamToList.viaStream ss 20 0.043 ± 0.006 s/op
当然,领域专家的第一个问题是基准测试程序是否正确?测试类在 MacOS 上执行。请让我知道所需的任何进一步详细信息。
后续,据我从读数中推断, Stream.toList 的平均时间、吞吐量和采样时间看起来比 Collectors.toList 好。这样的理解正确吗?

最佳答案

Stream::toList 建立在 toArray 之上,而不是 collect 之上。 toArray 中有许多优化,使其可能比收集更快,尽管这在很大程度上取决于细节。如果流管道(从源到最终中间操作)是 SIZED ,则可以预先调整目标数组的大小(而不是像 toList 收集器必须做的那样潜在地重新分配。)如果管道进一步是 SUBSIZED ,那么并行执行不仅可以预先调整结果的大小数组,但可以计算每个分片的精确偏移量,因此每个子任务都可以将其结果放在正确的位置,从而无需将中间结果复制到最终结果中。
因此,根据细节, toList 可能比 collect 快得多。

关于java - Stream.toList() 会比 Collectors.toList() 表现得更好吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65741773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com