Last active
May 14, 2022 10:21
-
-
Save olegcherr/b62a09aba1bff643a049 to your computer and use it in GitHub Desktop.
Simple Kotlin micro-benchmarking tool (Android supported)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package azagroup.test | |
import java.util.ArrayList | |
/** | |
* Iterates provided by [callback] code [ITERATIONS]x[TEST_COUNT] times. | |
* Performs warming by iterating [ITERATIONS]x[WARM_COUNT] times. | |
*/ | |
fun simpleMeasureTest( | |
ITERATIONS: Int = 1000, | |
TEST_COUNT: Int = 10, | |
WARM_COUNT: Int = 2, | |
callback: ()->Unit | |
) { | |
val results = ArrayList<Long>() | |
var totalTime = 0L | |
var t = 0 | |
println("$PRINT_REFIX -> go") | |
while (++t <= TEST_COUNT + WARM_COUNT) { | |
val startTime = System.currentTimeMillis() | |
var i = 0 | |
while (i++ < ITERATIONS) | |
callback() | |
if (t <= WARM_COUNT) { | |
println("$PRINT_REFIX Warming $t of $WARM_COUNT") | |
continue | |
} | |
val time = System.currentTimeMillis() - startTime | |
println(PRINT_REFIX+" "+time.toString()+"ms") | |
results.add(time) | |
totalTime += time | |
} | |
results.sort() | |
val average = totalTime / TEST_COUNT | |
val median = results[results.size / 2] | |
println("$PRINT_REFIX -> average=${average}ms / median=${median}ms") | |
} | |
/** | |
* Used to filter console messages easily | |
*/ | |
private val PRINT_REFIX = "[TimeTest]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple tool that can help you to measure the performance of your code in Kotlin.
Usage
With additional parameters:
Output
To make your life easier, create a new console output filter.
Here is my filter in Android Studio:
License
Use this code wherever you like.