Last active
June 15, 2018 13:49
-
-
Save dghubble/8810758 to your computer and use it in GitHub Desktop.
Scala String to Long check via regex vs cast
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
def time[A](a: => A) = { | |
val now = System.nanoTime | |
val result = a | |
val micros = (System.nanoTime - now) / 1000 | |
println("%d microseconds".format(micros)) | |
result | |
} | |
def validIdByRegex(id: String): Boolean = { | |
val idPattern = """[0-9]+""".r | |
idPattern.pattern.matcher(id).matches() | |
} | |
def validIdByCast(id: String): Boolean = { | |
try { | |
id.toLong | |
true | |
} catch { | |
case e: Throwable => false | |
} | |
} | |
def randomString(characters: String)(n: Int): String = | |
Stream.continually(scala.util.Random.nextInt(characters.size)).map(characters).take(n).mkString | |
def randomSometimesIdString(n: Int) = | |
randomString("abcd0123456789")(n) | |
val userIds = (0 until 1000) map { _ => randomSometimesIdString(10) } | |
println("validate by regex") | |
time { | |
userIds map {userId => | |
validIdByRegex(userId) | |
} | |
} | |
println("validate by cast") | |
time { | |
userIds map {userId => | |
validIdByCast(userId) | |
} | |
} | |
// results | |
// validate by regex | |
// 29033 microseconds | |
// validate by cast | |
// 13845 microseconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone accidentally finding this - it's non conclusive
Please do not make any conclusions based on this.
Just by extracting the regex out of the function you will get something like