Created
November 15, 2017 04:07
Star
You must be signed in to star a gist
Kotlin benchmark String.format vs String.replace
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
import org.apache.commons.lang.RandomStringUtils | |
import java.util.* | |
fun main(args: Array<String>) { | |
val tryCount = 1000000 | |
val formatStart = Date() | |
for (i in 1..tryCount) { | |
var string = "%s%s%s" | |
string += RandomStringUtils.randomAlphabetic(10) | |
val aa = string.format(RandomStringUtils.randomAlphabetic(3), RandomStringUtils.randomAlphabetic(3), RandomStringUtils.randomAlphabetic(3)) | |
println(aa) | |
} | |
val formatEnd = Date() | |
val formatTIme = formatEnd.time - formatStart.time | |
val replaceStart = Date() | |
for (i in 1..tryCount) { | |
var string = "%{h}%{s}%{f}" | |
string += RandomStringUtils.randomAlphabetic(10) | |
val aa = string.replace("%{h}", RandomStringUtils.randomAlphabetic(3)).replace("%{s}", RandomStringUtils.randomAlphabetic(3)).replace("%{f}", RandomStringUtils.randomAlphabetic(3)) | |
println(aa) | |
} | |
val replaceEnd = Date() | |
val replaceTime = replaceEnd.time - replaceStart.time | |
println("format end") | |
println(formatTIme) | |
println("replace end") | |
println(replaceTime) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace thought replacing warm-up of jvm was dominant by replace.