Last active
August 25, 2021 13:40
-
-
Save ryanlecompte/5210745 to your computer and use it in GitHub Desktop.
mutable.ArrayBuffer vs immutable.Vector
This file contains 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
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
Making things a little more interesting than just inserting values into the collections (note the N is reduced here to 50,000):
gives the following result: