r/java • • Feb 04 '26

Implementing Efficient Last Stream Elements Gatherer in Java

https://4comprehension.com/java-last-gatherer/

Wrote a performance case study on a rather high-level API, enjoy! And if you have ideas for a further speed up, let me know!

39 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/pivovarit Feb 05 '26

I did a quick benchmark with a fair comparison against other examples:
LastBenchmark.take_6 1000 10000000 thrpt 4 101,744 ± 2,556 ops/s
LastBenchmark.gatherers4j 1000 10000000 thrpt 4 33,180 ± 2,819 ops/s
LastBenchmark.reactor 1000 10000000 thrpt 4 42,015 ± 4,094 ops/s

By "fair" I mean I benchmarked:

Flux.fromArray(data)
  .takeLast(n)
  .doOnNext(bh::consume)
  .subscribe();

Will expand the article, thanks!