Skip to content

Instantly share code, notes, and snippets.

@maiha
Created May 15, 2010 02:45
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 maiha/401958 to your computer and use it in GitHub Desktop.
Save maiha/401958 to your computer and use it in GitHub Desktop.
def bench(code: => Any, times: Int = 1) {
val start = System.nanoTime
for(_ <- 1 to times) code
println(format("%.4f", (System.nanoTime - start)/1000000000.0))
}
scala> bench( 1+1 )
0.0000
scala> bench( 1+1, 10000)
0.0020
scala> bench( List("a"->"b"), 1000000 )
0.2600
scala> bench( Set("a"->"b"), 1000000 )
0.2170
scala> bench( Map("a"->"b"), 1000000 )
0.1610
scala> bench( ("a"->"b"), 1000000 )
0.0530
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment