Created
September 12, 2009 15:18
-
-
Save NicolasT/185876 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
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