Skip to content

Instantly share code, notes, and snippets.

@cbruegg
Created June 16, 2016 10:09
Show Gist options
  • Save cbruegg/8523425a1756b076d44332c4748626c9 to your computer and use it in GitHub Desktop.
Save cbruegg/8523425a1756b076d44332c4748626c9 to your computer and use it in GitHub Desktop.
package a
val pattern = "aba"
val text = "ababa".repeat(100000)
val runs = 1000
fun occTest(): Long {
// Warmup
repeat(10) {
text.occurrencesOf(pattern, ignoreCase = false, matchOverlapping = true).forEach { }
}
val startTime = System.currentTimeMillis()
repeat(runs) {
text.occurrencesOf(pattern, ignoreCase = false, matchOverlapping = true).forEach { }
}
val endTime = System.currentTimeMillis()
return (endTime - startTime)
}
fun regexTest(): Long {
// Warmup
val regex = Regex.fromLiteral(pattern)
repeat(10) {
for (i in 0..text.length - 1) {
regex.find(text, i)
}
}
val startTime = System.currentTimeMillis()
repeat(runs) {
for (i in 0..text.length - 1) {
regex.find(text, i)
}
}
val endTime = System.currentTimeMillis()
return (endTime - startTime)
}
fun main(args: Array<String>) {
val occ = occTest()
val reg = regexTest()
print("occ: $occ ms, regex: $reg ms")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment