Skip to content

Instantly share code, notes, and snippets.

@rxin
Last active December 19, 2015 03:49
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 rxin/5893121 to your computer and use it in GitHub Desktop.
Save rxin/5893121 to your computer and use it in GitHub Desktop.
Scala collection insert performance (2.9.3)
// 1001 381 384 384 383 384 407 404 409 407
object ArrayBufferBenchmark extends scala.testing.Benchmark {
def run = {
val len = 10 * 1000 * 1000
val a = new scala.collection.mutable.ArrayBuffer[Int](len)
var i = 0
while (i < len) {
a += i
i += 1
}
println(a(100))
}
}
// 962 384 371 364 371 393 396 386 388 386
object ArrayListBenchmark extends scala.testing.Benchmark {
def run = {
val len = 10 * 1000 * 1000
val a = new java.util.ArrayList[Int](len)
var i = 0
while (i < len) {
a.add(i)
i += 1
}
println(a.get(100))
}
}
// 939 366 359 356 361 357 382 378 382 381
object ArrayBoxingBenchmark extends scala.testing.Benchmark {
def run = {
val len = 10 * 1000 * 1000
val a = new Array[java.lang.Integer](len)
var i = 0
while (i < len) {
a(i) = i
i += 1
}
println(a(100))
}
}
// 133 56 57 58 57 58 57 58 56 57
object ArrayBoxingOnlyBenchmark extends scala.testing.Benchmark {
def run = {
val len = 10 * 1000 * 1000
val a = new Array[Int](len)
var i = 0
while (i < len) {
a(i) = Integer.valueOf(i)
i += 1
}
println(a(100))
}
}
// 29 18 13 12 15 14 12 15 13 14
object ArrayBenchmark extends scala.testing.Benchmark {
def run = {
val len = 10 * 1000 * 1000
val a = new Array[Int](len)
var i = 0
while (i < len) {
a(i) = i
i += 1
}
println(a(100))
}
}
// 151 72 67 65 65 66 64 65 63 64
object ArrayBuilderBenchmark extends scala.testing.Benchmark {
def run = {
val len = 10 * 1000 * 1000
val a = scala.collection.mutable.ArrayBuilder.make[Int]
a.sizeHint(len)
var i = 0
while (i < len) {
a += i
i += 1
}
println(a.result()(100))
}
}
// 58 34 14 14 14 16 14 17 18 14
object FastUtilBenchmark extends scala.testing.Benchmark {
def run = {
val len = 10 * 1000 * 1000
val a = new it.unimi.dsi.fastutil.ints.IntArrayList(len)
var i = 0
while (i < len) {
a.add(i)
i += 1
}
println(a.elements()(100))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment