How to benchmark a Kotlin Program?

前端 未结 3 1363
悲&欢浪女
悲&欢浪女 2021-02-08 20:53

Are there any tools available the can help benchmark some code in Kotlin?

I can use something similar to the approaches suggested here: http://www.ibm.com/developerworks

3条回答
  •  执笔经年
    2021-02-08 21:18

    All low level benchmarking tools are the same that you would use for Java, since the byte-code is identical.

    If by "Kotlin native tools" you mean syntactic sugar for measuring (relative) performance, then stdlib provides measureTimeMillis and measureNanoTime:

    fun main(args: Array) {
        val benchmark = measureNanoTime {
            // some slow code here
        }
    }
    

    The results may vary from run to run for obvious reasons, but it may help to estimate relative performance.

提交回复
热议问题