Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Created September 12, 2009 15:18
Show Gist options
  • Save NicolasT/185876 to your computer and use it in GitHub Desktop.
Save NicolasT/185876 to your computer and use it in GitHub Desktop.
object Profiler {
def apply[A](report: Long => Unit)(fun: => A): A = {
val start = System.currentTimeMillis()
val result = fun
val time = System.currentTimeMillis() - start
report(time)
result
}
def PrintProfiler[A](fun: => A) =
apply((t: Long) => println("Execution time: " + t + "ms"))(fun)
}
object Demo {
import Profiler.PrintProfiler
def test(): Int = { Thread.sleep(1000); 123 }
def main(args: Array[String]): Unit = {
val res = PrintProfiler(test)
println("Result: " + res)
Profiler((t: Long) => println("Execution time: " + t + "ms")) {
Thread.sleep(500)
}
PrintProfiler {
Thread.sleep(200)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment