Skip to content

Instantly share code, notes, and snippets.

@john-kurkowski
Created January 4, 2012 19:29
Show Gist options
  • Save john-kurkowski/1561609 to your computer and use it in GitHub Desktop.
Save john-kurkowski/1561609 to your computer and use it in GitHub Desktop.
In addition to Scala's Array.size gotcha, prefer .length to .size with ListBuffers
def timeit(fn: () => Any, times: Int = 100000) {
val start = System.currentTimeMillis
for (_ <- 0 until times) {
fn()
}
val end = System.currentTimeMillis
println((end - start) + "ms")
}
val n = 10000
val listBuffer = collection.mutable.ListBuffer(0.until(10000).toSeq:_*)
timeit(() => listBuffer.size)
timeit(() => listBuffer.length)
// From my computer:
//
// $ scala ListBufferSizeVsLength.scala
// 2984ms
// 9ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment