How to benchmark a Kotlin Program?

早过忘川 提交于 2020-01-22 12:23:47

问题


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/java/library/j-benchmark1/index.html - but I was wondering if there were any Kotlin native tools, so that i wouldn't necessarily have to reinvent the wheel!


回答1:


For benchmarking use JMH. This framework can help you write most relevant benchmark, and know a lot about how JVM works. There are old project on github, but i hope you can just update versions and you good.




回答2:


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<String>) {
    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.




回答3:


Here is a simple micro-benchmarking tool in Kotlin: https://gist.github.com/olegcherr/b62a09aba1bff643a049

Usage is simple:

simpleMeasureTest {
  // your code for the benchmark
}

Also you can configure testing, for example:

simpleMeasureTest(ITERATIONS = 1000, TEST_COUNT = 10, WARM_COUNT = 2) {
  // your code for the benchmark
}

After you run this code go into the console. The output will be looking like this:

I/System.out: [TimeTest] -> go
I/System.out: [TimeTest] Warming 1 of 2
I/System.out: [TimeTest] Warming 2 of 2
I/System.out: [TimeTest] 770ms
I/System.out: [TimeTest] 784ms
I/System.out: [TimeTest] 788ms
I/System.out: [TimeTest] 881ms
I/System.out: [TimeTest] 802ms
I/System.out: [TimeTest] 794ms
I/System.out: [TimeTest] 789ms
I/System.out: [TimeTest] 786ms
I/System.out: [TimeTest] 750ms
I/System.out: [TimeTest] 762ms
I/System.out: [TimeTest] -> average=790ms / median=788ms

To make your life easier, create a new console output filter. Here is my filter in Android Studio:



来源:https://stackoverflow.com/questions/35930456/how-to-benchmark-a-kotlin-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!