-
-
Save phucnh/c4bc393ca654873cacb9ad829694ef92 to your computer and use it in GitHub Desktop.
mutable.ArrayBuffer vs immutable.Vector
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
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