Large Sequences performing worse than large lists

So in the Kotlin in Action book, it talks about using Sequences when you have a large data set so you don’t incur the larger memory in dealing with each filter map function compared to using a standard List or Iterable.

In my gist CollectionAndSequence.kt · GitHub I wrote some code to average out over 1000 iterations of running filter map from 100-1,000,000 times
I’m not sure why this is the case, but List came out on top for larger sets on average instead of smaller sets of 100.

fun squareNumbers(range: CollectionRange): List<Int> = range.rangeList.filter { it % 3 == 0 }.map { it * it }.filter { it % 4 == 0 }.map { it * 3 }.filter { it % 4 == 0 }

fun squareNumbersSequence(range: CollectionRange) = range.rangeList.asSequence().filter { it % 3 == 0 }.map { it * it }.filter { it % 4 == 0 }.map { it * 3 }.filter { it % 4 == 0 }.toList()

I did filter{}.map{}.filter{}.map{}.filter{} as the list of operations. Can someone shed some light why Sequences perform slower than lists for filter map compared to List Iterables?

The big diference is that the values of the Sequence are evaluated lazily so it does’t use a huge amount of memory. The performance of the List might be better but you can run out of memory easily depending on the amount/size of elements in the list.

1 Like