Skip to content

Instantly share code, notes, and snippets.

@nihsmik
nihsmik / main.kt
Created December 28, 2020 09:24
Kotlin Performance Test: Iterable<T>.map
import kotlin.time.Duration
import kotlin.time.measureTime
fun main() {
val durations = mutableListOf<Duration>()
repeat(10) {
val duration = measureTime { (0..1_000_000).map { it + 1 } }
durations += duration
}
println(durations)
@nihsmik
nihsmik / main.kt
Created December 28, 2020 09:25
Kotlin Performance Test: Iterable<T>.forEach
import kotlin.time.Duration
import kotlin.time.measureTime
fun main() {
val durations = mutableListOf<Duration>()
repeat(10) {
val duration = measureTime { (0..1_000_000).forEach { it + 1 } }
durations += duration
}
println(durations)
@nihsmik
nihsmik / main.kt
Created December 28, 2020 09:30
Iterable<T>.map vs Iterable<T>.forEach
import kotlin.time.Duration
import kotlin.time.measureTime
fun main() {
val mapResults = mutableListOf<Duration>()
val forEachResults = mutableListOf<Duration>()
repeat(10) {
mapResults.add(measureTime { (0..1_000_000).map { it + 1 } })
forEachResults.add(measureTime { (0..1_000_000).forEach { it + 1 } })
}