How to benchmark a Kotlin Program?

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

提交回复
热议问题