I'm happy enough with Guava on Java 8 - are there any performance benefits or pitfalls in migrating to Streams for sequential code?
Yes, streams are sometimes slower than loops, but they can also be equally fast; it depends on the circumstances. The point to take home is that sequential streams are no faster than loops.
In Java8 Streams, performance is achieved by parallelism, laziness, and using short-circuit operations, but there is a downside as well, and we need to be very cautious while choosing Streams, as it may degrade the performance of your application. Let us look at these factors which are meant for Streams' performance.
Twice as better than a traditional for loop with an index int. Among the Java 8 methods, using parallel streams proved to be more effective.
I've started a project on GitHub to play with this.
Initial results are surprisingly positive for Streams - for an identity map over strings, Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode) on 2014 MacBookAir 1.7Ghz i7, code version 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
I'm still getting my head around Google calipers, but it seems to suggest that Streams are faster than Guava, and not much slower than a straight for loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With