Created
June 16, 2016 10:09
-
-
Save cbruegg/8523425a1756b076d44332c4748626c9 to your computer and use it in GitHub Desktop.
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
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