Skip to content

Instantly share code, notes, and snippets.

@chemacortes
Last active February 3, 2024 14:35
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 chemacortes/a4e25af1fed81e3d097eb75e44779f79 to your computer and use it in GitHub Desktop.
Save chemacortes/a4e25af1fed81e3d097eb75e44779f79 to your computer and use it in GitHub Desktop.
Benchmarks in scala
def benchmarks[A](f: => A) =
inline def memory() = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
val t0 = System.nanoTime()
val m0 = memory()
val ret = f
val t1 = System.nanoTime()
val m1 = memory()
println(s"time: ${(t1-t0) / 1e6} ms")
println(s"mem: ${(m1-m0) / 1_000_000_000.0} GB")
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment