Skip to content

Instantly share code, notes, and snippets.

@pauca
Last active January 22, 2019 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauca/a7b3264d4102f5d1dec4 to your computer and use it in GitHub Desktop.
Save pauca/a7b3264d4102f5d1dec4 to your computer and use it in GitHub Desktop.
measure and show elapsed time
// 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