是否有任何基准比较Java 8 Streams和Guava Iterables?

2022-09-03 02:59:00

我对Java 8上的Guava非常满意 - 迁移到Streams进行顺序代码是否有任何性能优势或陷阱?


答案 1

我已经在GitHub上启动了一个项目来玩这个。

对于 Streams 来说,初始结果出人意料地积极 - 对于字符串上的身份映射,2014 年 MacBookAir 1.7Ghz i7 上的 Java HotSpot(TM) 64 位服务器 VM(构建 25.25-b02,混合模式),代码版本 https://github.com/dmcg/iterables-v-streams#ea8498ee0627fc59834001a837fa92fba4bcf47ebcf47e

Experiment selection: 
  Benchmark Methods:   [guava, iterate, streams]
  Instruments:   [allocation, runtime]
  User parameters:   {}
  Virtual machines:  [default]
  Selection type:    Full cartesian product

This selection yields 6 experiments.
Trial Report (1 of 6):
  Experiment {instrument=allocation, benchmarkMethod=iterate, vm=default, parameters={}}
  Results:
    bytes(B): min=4072.00, 1st qu.=4072.00, median=4072.00, mean=4072.00, 3rd qu.=4072.00, max=4072.00
    objects: min=3.00, 1st qu.=3.00, median=3.00, mean=3.00, 3rd qu.=3.00, max=3.00
Trial Report (2 of 6):
  Experiment {instrument=allocation, benchmarkMethod=guava, vm=default, parameters={}}
  Results:
    bytes(B): min=15104.00, 1st qu.=15104.00, median=15104.00, mean=15104.00, 3rd qu.=15104.00, max=15104.00
    objects: min=17.00, 1st qu.=17.00, median=17.00, mean=17.00, 3rd qu.=17.00, max=17.00
Trial Report (3 of 6):
  Experiment {instrument=allocation, benchmarkMethod=streams, vm=default, parameters={}}
  Results:
    bytes(B): min=15272.00, 1st qu.=15272.00, median=15272.00, mean=15527.64, 3rd qu.=15432.00, max=17252.80
    objects: min=20.00, 1st qu.=20.00, median=20.00, mean=25.00, 3rd qu.=26.00, max=53.00
Trial Report (4 of 6):
  Experiment {instrument=runtime, benchmarkMethod=guava, vm=default, parameters={}}
  Results:
    runtime(ns): min=13365.32, 1st qu.=13660.61, median=13802.51, mean=13961.91, 3rd qu.=14445.46, max=14715.34
Trial Report (5 of 6):
  Experiment {instrument=runtime, benchmarkMethod=iterate, vm=default, parameters={}}
  Results:
    runtime(ns): min=9952.47, 1st qu.=10892.64, median=11372.35, mean=11243.07, 3rd qu.=11785.48, max=12024.76
Trial Report (6 of 6):
  Experiment {instrument=runtime, benchmarkMethod=streams, vm=default, parameters={}}
  Results:
    runtime(ns): min=10527.26, 1st qu.=11051.70, median=11747.29, mean=11631.15, 3rd qu.=12205.97, max=12581.39
Collected 81 measurements from:
  2 instrument(s)
  2 virtual machine(s)
  3 benchmark(s)

Execution complete: 1.188 min.
Results have been uploaded. View them at: https://microbenchmarks.appspot.com/runs/d2c7f83b-2cfa-4217-ab0b-e8d506eaa85c

我仍然在研究谷歌卡钳,但它似乎表明Streams比Guava更快,并且比直接循环慢不了多少。


答案 2

推荐