Last active
July 14, 2020 06:03
-
-
Save toantran-ea/2ffb1e8da9b817bc0efbddcb946e6912 to your computer and use it in GitHub Desktop.
sequence benchmarking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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