Skip to content

Instantly share code, notes, and snippets.

@igorwojda
Last active April 18, 2018 12:59
Show Gist options
  • Save igorwojda/65c497c15645bb75f5916ecce695fff6 to your computer and use it in GitHub Desktop.
Save igorwojda/65c497c15645bb75f5916ecce695fff6 to your computer and use it in GitHub Desktop.
fun main(args: Array<String>) {
val range = 1..10000
logTime("benchmarkPlusOperator") { benchmarkPlusOperator(range) }
logTime("benchmarkStringAppend") { benchmarkStringAppend(range) }
}
fun benchmarkPlusOperator(range:IntRange) {
var str = ""
range.forEach { str += "abc" }
}
fun benchmarkStringAppend(range: IntRange) {
val stringBuilder = StringBuilder()
range.forEach { stringBuilder.append("abc") }
stringBuilder.toString()
}
inline fun logTime(name:String, func:() -> Unit) {
val startTime = System.currentTimeMillis()
func()
val endTime = System.currentTimeMillis()
println("$name execution duration: ${ endTime - startTime }ms")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment