3 ms·
Did a similar benchmark in Kotlin using co-routines. import kotlin.time.Duration.Companion.milliseconds import kotlin.time.measureTime import kotli
by jillesvangurp 2y ago
Did a similar benchmark in Kotlin using co-routines.
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.measureTime
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
suspend fun main() {
measureTime {
coroutineScope {
(0..1000000).map {
async {
delay(1.milliseconds)
}
}.awaitAll()
}
}.let { t ->
println("Took $t")
val runtime = Runtime.getRuntime()
val maxHeapSize = runtime.maxMemory()
val allocatedHeapSize = runtime.totalMemory()
val freeHeapSize = runtime.freeMemory()
println("Max Heap: ${maxHeapSize / 1024 / 1024} MB")
println("Allocated Heap: ${allocatedHeapSize / 1024 / 1024} MB")
println("Free Heap: ${freeHeapSize / 1024 / 1024} MB")
}
}
This produces the following output:
Took 1.597011084s
Max Heap: 4096 MB
Allocated Heap: 2238 MB
Free Heap: 1548 MB
So whatever is needed to load classes and a million co-routines with some heap state. Of course the whole thing isn't doing any work and this isn't much of a benchmark. And of course if I run it with kotlin-js it actually ends up using promises. So, it's not going to be any better there than on the JVM.