Last active
April 18, 2018 12:59
-
-
Save igorwojda/65c497c15645bb75f5916ecce695fff6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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