Slow range forEach in Kotlin

前端 未结 3 1516
南方客
南方客 2020-12-18 10:51

I used the following code to measure performance of different syntax constructions in Kotlin

fun time(what: String, body: () -> Int) {
    val start =          


        
3条回答
  •  感情败类
    2020-12-18 11:22

    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

提交回复
热议问题