I expected that simple intermediate stream operations, such as limit()
, have very little overhead. But the difference in throughput between these examples is actually significant:
final long MAX = 5_000_000_000L;
LongStream.rangeClosed(0, MAX)
.count();
// throughput: 1.7 bn values/second
LongStream.rangeClosed(0, MAX)
.limit(MAX)
.count();
// throughput: 780m values/second
LongStream.rangeClosed(0, MAX)
.limit(MAX)
.limit(MAX)
.count();
// throughput: 130m values/second
LongStream.rangeClosed(0, MAX)
.limit(MAX)
.limit(MAX)
.limit(MAX)
.count();
// throughput: 65m values/second
I am curious: What is the reason for the quickly degrading throughput? Is it a consistent pattern with chained stream operations or my test setup? (I did not use JMH so far, just set up a quick experiment with a stopwatch)