Skip to content

Instantly share code, notes, and snippets.

@phucnh
Forked from ryanlecompte/gist:5210745
Created August 25, 2019 02:16
Show Gist options
  • Save phucnh/c4bc393ca654873cacb9ad829694ef92 to your computer and use it in GitHub Desktop.
Save phucnh/c4bc393ca654873cacb9ad829694ef92 to your computer and use it in GitHub Desktop.
mutable.ArrayBuffer vs immutable.Vector
import scala.collection._
import com.twitter.util._
scala> val buffer = mutable.ArrayBuffer.empty[Int]
scala> println(Time.measure { (0 to 50000000).foreach { buffer += _ } }.inMillis)
17610
scala> var vector = immutable.Vector.empty[Int]
scala> println(Time.measure { (0 to 50000000).foreach { vector :+= _ } }.inMillis)
7865
// do it again, just in case
scala> val buffer = mutable.ArrayBuffer.empty[Int]
scala> println(Time.measure { (0 to 50000000).foreach { buffer += _ } }.inMillis)
24742
// let's try a java.util.ArrayList
val buffer2 = new java.util.ArrayList[Int]
scala> println(Time.measure { (0 to 50000000).foreach { buffer2.add(_) } }.inMillis)
7923
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment