Benchmarking Kotlin Sequences

AJ Alt
AJ Alt
Sep 19, 2018 · 5 min read

What are Sequences?

Benchmarking setup

list.filter { true }.map { it }.filter { true }.map { it }
list.asSequence()
.filter { true }.map { it }.filter { true }.map { it }
.toList()

Results

Time to perform a number of operations on a list of integers. All three axes are log-scale. Lower is better. “chain length” is the number of filter and map operations chained together.
Time per item to perform a number of operations on a list of 1 million ints. Lower is better.
Time per item for lists minus time per item for Sequences. Positive values mean that lists are slower, and negative values mean the Sequences are slower.
list.filter { true }.map { Thread.sleep(0, 5000); it }
Total time to run with a map function that takes 5µs
list.map { Thread.sleep(0, 5000); it }.first { it > 2 }

Conclusion

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade