Last active
January 11, 2020 13:57
-
-
Save fukusaka/cdca81c890b71a5da5c6bb29ff61fadd to your computer and use it in GitHub Desktop.
ちょっと Kotlin の Iterator と Sequence の比較をしてみた
This file contains hidden or 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
import kotlin.system.measureNanoTime | |
fun buildUseList(list: List<Int>, half: Int): List<String> { | |
return list | |
.filter { it < half } | |
.filter { it % 2 == 0 } | |
.map { it.toString() } | |
.take(3) | |
} | |
fun buildUseSequence(list: List<Int>, half: Int): List<String> { | |
return list | |
.asSequence() | |
.filter { it < half } | |
.filter { it % 2 == 0 } | |
.map { it.toString() } | |
.take(3) | |
.toList() | |
} | |
fun main() { | |
val dataSet = | |
listOf(1_000_000, 100_000, 10_000, 1_000, 200, 100, 10) | |
.associateWith { (1..it).toList() } | |
// warmup | |
val warm = 1_000_000 to (1..1_000_000).toList() | |
buildUseList(warm.second, warm.first / 2) | |
buildUseSequence(warm.second, warm.first / 2) | |
// end of warmup | |
dataSet.forEach { | |
println("useList %8d %8d nsec".format(it.key, measureNanoTime { buildUseList(it.value, it.key / 2) })) | |
println("useSequence %8d %8d nsec".format(it.key, measureNanoTime { buildUseSequence(it.value, it.key / 2) })) | |
} | |
} |
This file contains hidden or 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
検証した Android 端末 | |
Pixel 3 XL / Android 10 (ART) | |
2020-01-11 22:51:03.871 18459-18459/com.sample.app001 I/System.out: useList 1000000 162565537 nsec | |
2020-01-11 22:51:03.871 18459-18459/com.sample.app001 I/System.out: useSequence 1000000 116718 nsec | |
2020-01-11 22:51:03.883 18459-18459/com.sample.app001 I/System.out: useList 100000 11641147 nsec | |
2020-01-11 22:51:03.884 18459-18459/com.sample.app001 I/System.out: useSequence 100000 102969 nsec | |
2020-01-11 22:51:03.885 18459-18459/com.sample.app001 I/System.out: useList 10000 1226875 nsec | |
2020-01-11 22:51:03.886 18459-18459/com.sample.app001 I/System.out: useSequence 10000 116875 nsec | |
2020-01-11 22:51:03.886 18459-18459/com.sample.app001 I/System.out: useList 1000 166250 nsec | |
2020-01-11 22:51:03.886 18459-18459/com.sample.app001 I/System.out: useSequence 1000 64271 nsec | |
2020-01-11 22:51:03.887 18459-18459/com.sample.app001 I/System.out: useList 200 54948 nsec | |
2020-01-11 22:51:03.887 18459-18459/com.sample.app001 I/System.out: useSequence 200 63177 nsec | |
2020-01-11 22:51:03.887 18459-18459/com.sample.app001 I/System.out: useList 100 49688 nsec | |
2020-01-11 22:51:03.887 18459-18459/com.sample.app001 I/System.out: useSequence 100 62604 nsec | |
2020-01-11 22:51:03.888 18459-18459/com.sample.app001 I/System.out: useList 10 42448 nsec | |
2020-01-11 22:51:03.888 18459-18459/com.sample.app001 I/System.out: useSequence 10 60364 nsec | |
This file contains hidden or 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
検証したマシン/Java 環境 | |
macOS Catalina / Intel Core i9 2.9 GHz / 32GB | |
OpenJDK Runtime Environment Corretto-11.0.5.10.2 (build 11.0.5+10-LTS) | |
useList 1000000 60066750 nsec | |
useSequence 1000000 28238 nsec | |
useList 100000 1507380 nsec | |
useSequence 100000 23472 nsec | |
useList 10000 130913 nsec | |
useSequence 10000 43383 nsec | |
useList 1000 20584 nsec | |
useSequence 1000 17550 nsec | |
useList 200 8385 nsec | |
useSequence 200 16703 nsec | |
useList 100 6814 nsec | |
useSequence 100 16689 nsec | |
useList 10 15051 nsec | |
useSequence 10 28847 nsec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment