-
-
Save cr0t/1183762 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
import Benchmark._ | |
object App { | |
def main(args: Array[String]): Unit = { | |
benchmark(100000){ | |
report("foo"){ foo() } :: | |
report("bar"){ bar() } :: | |
Nil | |
} | |
} | |
} |
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
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 _) | |
} |
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
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