Skip to content

Instantly share code, notes, and snippets.

@simon-tse-hs
Last active June 28, 2017 17:30
Show Gist options
  • Save simon-tse-hs/7dce88df4c0870b8d580205c587ea6ac to your computer and use it in GitHub Desktop.
Save simon-tse-hs/7dce88df4c0870b8d580205c587ea6ac to your computer and use it in GitHub Desktop.
CollectionAndSequence.kt
package com.simon.sample
import com.simon.sample.CollectionRange.to100
import com.simon.sample.CollectionRange.to1000
import com.simon.sample.CollectionRange.to10000
import com.simon.sample.CollectionRange.to100000
import com.simon.sample.CollectionRange.to1000000
import kotlin.system.measureTimeMillis
enum class CollectionType {
List,
Sequence
}
enum class CollectionRange(val rangeList: List<Int>) {
to100((0..100).toList()),
to1000((0..1000).toList()),
to10000((0..10000).toList()),
to100000((0..100000).toList()),
to1000000((0..1000000).toList()),
}
fun printCollectionResults(type: CollectionType, range: CollectionRange, elapseTime: Double) {
println("${type.name} for range ${range.name} took - $elapseTime ms")
}
fun printSection(range: CollectionRange) = println("### List vs Sequence Average - $range")
fun printWinner(range: CollectionRange, listElapseTime: Double, sequenceElapseTime: Double) = println("Winner for range ${range.name} is ${if (listElapseTime < sequenceElapseTime) "List" else "Sequence "}")
fun averageList(range: CollectionRange) = (0..1000).toList().map { measureTimeMillis { squareNumbers(range) } }.average()
fun averageSequence(range: CollectionRange) = (0..1000).toList().map { measureTimeMillis { squareNumbersSequence(range) } }.average()
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()
fun runFilterMapComparison(range: CollectionRange) {
printSection(range)
val elapsedTimeList = averageList(range)
val elapsedTimeSequence = averageSequence(range)
printCollectionResults(CollectionType.List, range, elapsedTimeList)
printCollectionResults(CollectionType.Sequence, range, elapsedTimeSequence)
printWinner(range, elapsedTimeList, elapsedTimeSequence)
println("")
}
fun collectionFilterMapAverage() {
runFilterMapComparison(to100)
runFilterMapComparison(to1000)
runFilterMapComparison(to10000)
runFilterMapComparison(to100000)
runFilterMapComparison(to1000000)
}
fun main(args: Array<String>) {
println("Average")
collectionFilterMapAverage()
}
@simon-tse-hs
Copy link
Author

Prints out
Average

List vs Sequence Average - to100

List for range to100 took - 0.12487512487512488 ms
Sequence for range to100 took - 0.11988011988011989 ms
Winner for range to100 is Sequence

List vs Sequence Average - to1000

List for range to1000 took - 0.3756243756243756 ms
Sequence for range to1000 took - 0.0959040959040959 ms
Winner for range to1000 is Sequence

List vs Sequence Average - to10000

List for range to10000 took - 0.5744255744255744 ms
Sequence for range to10000 took - 0.6973026973026973 ms
Winner for range to10000 is List

List vs Sequence Average - to100000

List for range to100000 took - 2.99000999000999 ms
Sequence for range to100000 took - 4.654345654345654 ms
Winner for range to100000 is List

List vs Sequence Average - to1000000

List for range to1000000 took - 22.92107892107892 ms
Sequence for range to1000000 took - 47.64035964035964 ms
Winner for range to1000000 is List

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment