Created
January 4, 2012 19:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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