Skip to content

Instantly share code, notes, and snippets.

@eishay
Created December 30, 2013 17:29
Show Gist options
  • Save eishay/8185105 to your computer and use it in GitHub Desktop.
Save eishay/8185105 to your computer and use it in GitHub Desktop.
def matching(str: String): Boolean = str match {
case "another string" => false
case "yet another" => false
case "not this" => false
case "hello world" => true
case other => false
}
def iffing(str: String): Boolean = {
if (str == "another string") false
else if (str == "yet another") false
else if (str == "not this") false
else if (str == "hello world") true
else false
}
def matchBenchmark() = {
val startMatching = System.currentTimeMillis
var i = 0
val rounds = 1000000
while (i < rounds) {
matching("hello world")
matching("nothing here")
i = i + 1
}
def delta = System.currentTimeMillis - startMatching
println(s"time = $delta")
delta
}
def ifBenchmark() = {
val startIffing = System.currentTimeMillis
var i = 0
val rounds = 1000000
while (i < rounds) {
iffing("hello world")
iffing("nothing here")
i = i + 1
}
def delta = System.currentTimeMillis - startIffing
println(s"time = $delta")
delta
}
matchBenchmark()
matchBenchmark()
matchBenchmark()
matchBenchmark()
ifBenchmark()
ifBenchmark()
ifBenchmark()
ifBenchmark()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment