Skip to content

Instantly share code, notes, and snippets.

@dghubble
Last active June 15, 2018 13:49
Show Gist options
  • Save dghubble/8810758 to your computer and use it in GitHub Desktop.
Save dghubble/8810758 to your computer and use it in GitHub Desktop.
Scala String to Long check via regex vs cast
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
@letalvoj
Copy link

letalvoj commented Jun 15, 2018

For anyone accidentally finding this - it's non conclusive

  • regex is being compiled all over again
  • no warm up runs
  • no GC handling

Please do not make any conclusions based on this.

Just by extracting the regex out of the function you will get something like

validate by regex
8609 microseconds
validate by cast
11394 microseconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment