Skip to content

Instantly share code, notes, and snippets.

@overthink
Created August 15, 2013 15:36
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 overthink/6241841 to your computer and use it in GitHub Desktop.
Save overthink/6241841 to your computer and use it in GitHub Desktop.
O(n) size by default... great.
def time[R](block: => R): R = {
val t0 = System.currentTimeMillis
val result = block // call-by-name
val t1 = System.currentTimeMillis
println("Elapsed time: " + (t1 - t0) + "ms")
result
}
val list1 = 1 to 10000 toList
val ary1 = 1 to 10000 toArray
time { (1 to 1000000).foreach { _ => list1.size } } // Elapsed time: 22672ms
time { (1 to 1000000).foreach { _ => ary1.size } } // Elapsed time: 20ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment