How to benchmark a Kotlin Program?

前端 未结 3 1362
悲&欢浪女
悲&欢浪女 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:12

    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:

    0 讨论(0)
  • 2021-02-08 21:14

    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.

    0 讨论(0)
  • 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<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.

    0 讨论(0)
提交回复
热议问题