Skip to content

Instantly share code, notes, and snippets.

@Lundez
Last active Aug 25, 2020
Embed
What would you like to do?
Saving a file I used to test replacement methods for string (show-casing in a TIL)
import java.io.File
import kotlin.system.measureTimeMillis
object RegexTester {
val text = File("/home/londet/git/text-gen-kt/files/shakespeare.txt").readText()
val textSmall = text.take(1000)
val textLarge = text.repeat(10)
val regex = "[abc]".toRegex()
val charReplace = listOf('a', 'b', 'c')
val stringReplace = listOf("a", "b", "c")
@JvmStatic
fun main(args: Array<String>) {
println("Warming up JVM by running 10,000 iterations of each replacer on normal size.")
(1..10_000)
.forEach { regex.replace(text, "d") }
(1..10_000)
.forEach { charReplace.fold(text) { acc, ch -> acc.replace(ch, 'd') } }
(1..10_000)
.forEach { stringReplace.fold(text) { acc, ch -> acc.replace(ch, "d") } }
println("Warmup done!")
val regexSmall = measureTimeMillis { (1..1_000_000).forEach { regex.replace(textSmall, "d") } } / 1_000_000.0
val regexNormal = measureTimeMillis { (1..1_000).forEach { regex.replace(text, "d") } } / 1000.0
val regexLarge = measureTimeMillis { (1..10).forEach { regex.replace(textLarge, "d") } } / 10.0
// val regexLargeCompile = measureTimeMillis { (1..10).forEach { textLarge.replace("[abc]", "d") } } / 10.0
println("Regex Small (1000 characters, 1,000,000 avg): $regexSmall")
println("Regex Normal (4.5 million characters, 1000 avg): $regexNormal")
println("Regex Large (45 million characters, 10 avg): $regexLarge")
val charSmall = measureTimeMillis { (1..1_000_000).forEach { charReplace.fold(textSmall) { acc, ch -> acc.replace(ch, 'd') } } } / 1_000_000.0
val charNormal = measureTimeMillis { (1..1_000).forEach { charReplace.fold(text) { acc, ch -> acc.replace(ch, 'd') } } } / 1000.0
val charLarge = measureTimeMillis { (1..10).forEach { charReplace.fold(textLarge) { acc, ch -> acc.replace(ch, 'd') } } } / 10.0
println("CharReplace Small (1000 characters, 1,000,000 avg): $charSmall")
println("CharReplace Normal (4.5 million characters, 1000 avg): $charNormal")
println("CharReplace Large (45 million characters, 10 avg): $charLarge")
val stringSmall = measureTimeMillis { (1..1_000_000).forEach { stringReplace.fold(textSmall) { acc, ch -> acc.replace(ch, "d") } } } / 1_000_000.0
val stringNormal = measureTimeMillis { (1..1_000).forEach { stringReplace.fold(text) { acc, ch -> acc.replace(ch, "d") } } } / 1000.0
val stringLarge = measureTimeMillis { (1..10).forEach { stringReplace.fold(textLarge) { acc, ch -> acc.replace(ch, "d") } } } / 10.0
println("StringReplace Small (1000 characters, 1,000,000 avg): $stringSmall")
println("StringReplace Normal (4.5 million characters, 1000 avg): $stringNormal")
println("StringReplace Large (45 million characters, 10 avg): $stringLarge")
/**
Regex Small (1000 characters, 1,000,00 avg): 0.004949
Regex Normal (4.5 million characters, 1000 avg): 29.671
Regex Large (45 million characters, 10 avg): 361.8
CharReplace Small (1000 characters, 1,000,00 avg): 0.002752
CharReplace Normal (4.5 million characters, 1000 avg): 11.835
CharReplace Large (45 million characters, 10 avg): 117.0
StringReplace Small (1000 characters, 1,000,00 avg): 0.008692
StringReplace Normal (4.5 million characters, 1000 avg): 57.204
StringReplace Large (45 million characters, 10 avg): 588.1
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment