Created
December 30, 2013 17:29
-
-
Save eishay/8185105 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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