Skip to content

Instantly share code, notes, and snippets.

@cr0t
Forked from teamon/App.scala
Created August 31, 2011 15:02

Revisions

  1. @teamon teamon created this gist Mar 8, 2011.
    11 changes: 11 additions & 0 deletions App.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    import Benchmark._

    object App {
    def main(args: Array[String]): Unit = {
    benchmark(100000){
    report("foo"){ foo() } ::
    report("bar"){ bar() } ::
    Nil
    }
    }
    }
    22 changes: 22 additions & 0 deletions Benchmark.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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 _)
    }
    4 changes: 4 additions & 0 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    Name Time(s)
    ===============================
    foo 0.084000
    bar 0.082000