Skip to content

Instantly share code, notes, and snippets.

@cr0t
Forked from teamon/App.scala
Created August 31, 2011 15:02
Show Gist options
  • Save cr0t/1183762 to your computer and use it in GitHub Desktop.
Save cr0t/1183762 to your computer and use it in GitHub Desktop.
import Benchmark._
object App {
def main(args: Array[String]): Unit = {
benchmark(100000){
report("foo"){ foo() } ::
report("bar"){ bar() } ::
Nil
}
}
}
object Benchmark {
case class BenchmarkReport(name: String, f: () => Unit){
def run(n: Int) = {
val start = System.currentTimeMillis
(1 to n) foreach { i => f() }
val time = System.currentTimeMillis - start
(name, time)
}
}
def benchmark(n: Int)(reports: List[BenchmarkReport]) = {
val results = reports.map(_.run(n))
println("Name Time(s)")
println("===============================")
results.foreach { case (name, time) =>
printf("%-20s %10f\n", name, time / 1000.0)
}
results
}
def report(name: String)(f: => Unit) = BenchmarkReport(name, f _)
}
Name Time(s)
===============================
foo 0.084000
bar 0.082000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment