Last active
July 24, 2016 21:03
-
-
Save lbialy/086088c9558b88a64a2feb6b17bfd10b to your computer and use it in GitHub Desktop.
Recursive factorial in scala with performance metrics
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
import org.scalameter._ | |
object FactorialRunner { | |
def fact(v: BigInt): BigInt = { | |
def recFact(v: BigInt, acc: BigInt): BigInt = | |
if (v == 1) acc else recFact(v - 1, acc * v) | |
recFact(v, 1) | |
} | |
val standardConfig = config( | |
Key.exec.minWarmupRuns -> 20, | |
Key.exec.maxWarmupRuns -> 40, | |
Key.exec.benchRuns -> 25, | |
Key.verbose -> true | |
) withWarmer new Warmer.Default | |
def main(args: Array[String]): Unit = { | |
val meanTime = standardConfig measure { | |
fact(20000) | |
} | |
println(s"factorial of 20000 mean time: $meanTime ms") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires scalameter library in
build.sbt
:Execute via sbt console:
run-main FactorialRunner