Skip to content

Instantly share code, notes, and snippets.

@toantran-ea
Last active July 14, 2020 06:03
Show Gist options
  • Save toantran-ea/2ffb1e8da9b817bc0efbddcb946e6912 to your computer and use it in GitHub Desktop.
Save toantran-ea/2ffb1e8da9b817bc0efbddcb946e6912 to your computer and use it in GitHub Desktop.
sequence benchmarking
fun main(args: Array<String>) {
val result = mutableListOf<Pair<Long, Long>>()
repeat(100) {
result.add(task())
}
println("====================")
println("LIST TIME (ns) || SEQUENCE TIME (ns)")
result.forEach {
println(" ${it.first} || ${it.second} || delta ${it.first - it.second} ")
}
}
fun task(): Pair<Long, Long> {
val list = "The quick brown fox jumps over the lazy dog".repeat(100).split(" ")
val sequence = list.asSequence()
val listTime = measureNanoTime {
normalList(list)
}
val seqTime = measureNanoTime {
sequence(sequence)
}
return listTime to seqTime
}
fun normalList(list: List<String>) {
list.filter {
println("filter: $it");
it.length > 3
}
.map { it + it }
.map { it.length }
.take(4)
}
fun sequence(seq: Sequence<String>) {
seq.filter {
println("filter: $it");
it.length > 3
}
.map { it + it }
.map { it.length }
.take(4)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment