Last active
January 22, 2019 11:57
-
-
Save pauca/a7b3264d4102f5d1dec4 to your computer and use it in GitHub Desktop.
measure and show elapsed time
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
// from https://biercoff.com/easily-measuring-code-execution-time-in-scala/ | |
def time[R](block: => R): R = { | |
val t0 = System.nanoTime() | |
val result = block // call-by-name | |
val t1 = System.nanoTime() | |
println("Elapsed time: " + (t1 - t0) + "ns") | |
result | |
} | |
///// time{ 2+2} | |
import java.util.concurrent.TimeUnit | |
val start = System.nanoTime() | |
// something to do | |
val end = System.nanoTime() | |
val difference = end - start | |
println("Total execution time: " + | |
TimeUnit.NANOSECONDS.toHours(difference) + " hours " + | |
( TimeUnit.NANOSECONDS.toMinutes(difference) - TimeUnit.HOURS.toMinutes(TimeUnit.NANOSECONDS.toHours(difference))) + " min " + | |
( TimeUnit.NANOSECONDS.toSeconds(difference) - TimeUnit.MINUTES.toSeconds(TimeUnit.NANOSECONDS.toMinutes(difference))) + " sec " + | |
" - " + difference + " nSec (Total)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment