Simple listOf(..) function call takes immensly long, when called the first time
I noticed that building of a simple list can take immensly long. I use the following little test program:
var endTime: Long
var startTime:Long
var l:List<Int>
repeat(10) {
startTime = System.nanoTime()
l = listOf(1, 1, 1, 1)
endTime = System.nanoTime()
println(endTime - startTime)
}
It yields (all times in nano seconds).
14138100
2400
1000
900
900
900
1000
1200
1000
900
So, it levels out at around 1 mycro second, but the first time takes 14 milli seconds, which is really VERY long for this.
The above is reproducable reliably. Something happens, when calling the function the first time, as it appears. Is there any explanation and a remediation for this kind of effect?
I am working with IntelliJ IDEA and Kotlin obviously. Machine: ThinkPad P15 Gen 1/ T15g Gen1 with Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz.
Please sign in to leave a comment.
This is closed. I raised a case in the Kotlin forum: List initialisation takes long - Libraries - Kotlin Discussions (kotlinlang.org).
I got a comprehensive answer there. It is the 'warm-up' time the JVM needs.