Created
June 14, 2021 14:38
-
-
Save paulmr/2d2f3acf4e7033fe7e65e8114324b2c2 to your computer and use it in GitHub Desktop.
time a scala expression's execution
This file contains 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
// e.g., from the repl: | |
// scala> :load timer.scala | |
// [...] | |
// scala> timed(Thread.sleep(1000)) | |
// took 1001606189ns (1001ms) | |
def timed[T](block: => T): T = { | |
import java.time.Duration | |
val startTime = System.nanoTime() | |
val res = block | |
val endTime = System.nanoTime() | |
val took = Duration.ofNanos(endTime - startTime) | |
println(s"took ${took.toNanos}ns (${took.toMillis}ms)") | |
res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment