Skip to content

Instantly share code, notes, and snippets.

@lbialy
Last active July 24, 2016 21:03
Show Gist options
  • Save lbialy/086088c9558b88a64a2feb6b17bfd10b to your computer and use it in GitHub Desktop.
Save lbialy/086088c9558b88a64a2feb6b17bfd10b to your computer and use it in GitHub Desktop.
Recursive factorial in scala with performance metrics
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")
}
}
@lbialy
Copy link
Author

lbialy commented Jul 24, 2016

Requires scalameter library in build.sbt:

name := "Factorial with metrics"

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
  "com.storm-enroute" %% "scalameter-core" % "0.6",
  "com.storm-enroute" %% "scalameter" % "0.6" % "test"
)

Execute via sbt console: run-main FactorialRunner

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