I used the following code to measure performance of different syntax constructions in Kotlin
fun time(what: String, body: () -> Int) {
val start =
Thanks for this code. I was more interested in the performance difference between array and list and was a bit shocked to have 6 times as long running time. I thought the list is only slow in creation.
Above runtimes on my machine or comparison:
for in range: 55
for in collection: 57
forEach: 55
range forEach: 223
sum: 57
//code change
//val arr = IntArray(n) { rand.nextInt() }
val arr = List(n) { rand.nextInt() }
for in range: 383
for in collection: 367
forEach: 367
range forEach: 486
sum: 371