Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A quick and dirty experiment to compare the performance of scala immutable collections (Seq, List, Vector) of integers when accessing randomly, appending, prepending an element. See article http://danielasfregola.com/2015/06/15/which-immutable-scala-collection.
import scala.concurrent.duration._
import scala.util.Random
import collection.immutable.Seq
private def prettyPrint(text: String)(duration: Duration): Unit =
println(s"[$text] - ${duration}")
private def ranges(base: Int, n: Int): Seq[Range] = for {
i <- 1 until n
} yield 1 until math.pow(base,i).toInt
private def measureAvgExecTime[T](n: Int)(op: => T): Duration = {
def measureExecTime[T]: Long = {
val startTime = System.nanoTime()
op
val endTime = System.nanoTime()
(endTime - startTime)
}
val values: Seq[Long] = (1 until n).map(_ => measureExecTime)
values.sum / n nanoseconds
}
val Iterations: Int = 27
val Base: Int = 2
val Repeats: Int = 10
lazy val ranges: Seq[Range] = ranges(Base, Iterations)
var idx = 0
for {
range <- ranges
}{
val seq: Seq[Int] = range
val list: List[Int] = range.toList
val vector: Vector[Int] = range.toVector
def prettyPrintForSeq = prettyPrint("Seq") _
def prettyPrintForList = prettyPrint("List") _
def prettyPrintForVector = prettyPrint("Vector") _
def measureAvg = measureAvgExecTime(Repeats) _
val random: Int = Random.nextInt(range.size)
println(s"****** $Base^$idx elements *******")
println()
idx += 1
println("**** APPLY ****")
prettyPrintForSeq(measureAvg(seq(random)))
prettyPrintForList(measureAvg(list(random)))
prettyPrintForVector(measureAvg(vector(random)))
println()
println("**** APPENDING ****")
prettyPrintForSeq(measureAvg(seq :+ random))
prettyPrintForList(measureAvg(list :+ random))
prettyPrintForVector(measureAvg(vector :+ random))
println()
println("**** PREPENDING ****")
prettyPrintForSeq(measureAvg(random +: seq))
prettyPrintForList(measureAvg(random +: list))
prettyPrintForVector(measureAvg(random +: vector))
println()
}
println()
println("******** THE END ********")
println()
****** 2^0 elements *******
**** APPLY ****
[Seq] - 4617 nanoseconds
[List] - 3832 nanoseconds
[Vector] - 3418 nanoseconds
**** APPENDING ****
[Seq] - 6011 nanoseconds
[List] - 9777 nanoseconds
[Vector] - 10121 nanoseconds
**** PREPENDING ****
[Seq] - 6772 nanoseconds
[List] - 3095 nanoseconds
[Vector] - 9122 nanoseconds
****** 2^1 elements *******
**** APPLY ****
[Seq] - 928 nanoseconds
[List] - 682 nanoseconds
[Vector] - 589 nanoseconds
**** APPENDING ****
[Seq] - 2529 nanoseconds
[List] - 1412 nanoseconds
[Vector] - 3057 nanoseconds
**** PREPENDING ****
[Seq] - 2407 nanoseconds
[List] - 463 nanoseconds
[Vector] - 4447 nanoseconds
****** 2^2 elements *******
**** APPLY ****
[Seq] - 816 nanoseconds
[List] - 612 nanoseconds
[Vector] - 487 nanoseconds
**** APPENDING ****
[Seq] - 2633 nanoseconds
[List] - 1439 nanoseconds
[Vector] - 3095 nanoseconds
**** PREPENDING ****
[Seq] - 3188 nanoseconds
[List] - 385 nanoseconds
[Vector] - 4761 nanoseconds
****** 2^3 elements *******
**** APPLY ****
[Seq] - 4544 nanoseconds
[List] - 1111 nanoseconds
[Vector] - 889 nanoseconds
**** APPENDING ****
[Seq] - 8487 nanoseconds
[List] - 1923 nanoseconds
[Vector] - 3625 nanoseconds
**** PREPENDING ****
[Seq] - 3848 nanoseconds
[List] - 455 nanoseconds
[Vector] - 4544 nanoseconds
****** 2^4 elements *******
**** APPLY ****
[Seq] - 888 nanoseconds
[List] - 818 nanoseconds
[Vector] - 590 nanoseconds
**** APPENDING ****
[Seq] - 5712 nanoseconds
[List] - 1971 nanoseconds
[Vector] - 3447 nanoseconds
**** PREPENDING ****
[Seq] - 5695 nanoseconds
[List] - 500 nanoseconds
[Vector] - 4570 nanoseconds
****** 2^5 elements *******
**** APPLY ****
[Seq] - 899 nanoseconds
[List] - 1219 nanoseconds
[Vector] - 568 nanoseconds
**** APPENDING ****
[Seq] - 10636 nanoseconds
[List] - 2588 nanoseconds
[Vector] - 6542 nanoseconds
**** PREPENDING ****
[Seq] - 11111 nanoseconds
[List] - 461 nanoseconds
[Vector] - 5935 nanoseconds
****** 2^6 elements *******
**** APPLY ****
[Seq] - 840 nanoseconds
[List] - 3400 nanoseconds
[Vector] - 2740 nanoseconds
**** APPENDING ****
[Seq] - 17399 nanoseconds
[List] - 4394 nanoseconds
[Vector] - 5477 nanoseconds
**** PREPENDING ****
[Seq] - 18080 nanoseconds
[List] - 382 nanoseconds
[Vector] - 3730 nanoseconds
****** 2^7 elements *******
**** APPLY ****
[Seq] - 801 nanoseconds
[List] - 3006 nanoseconds
[Vector] - 499 nanoseconds
**** APPENDING ****
[Seq] - 37256 nanoseconds
[List] - 6005 nanoseconds
[Vector] - 4974 nanoseconds
**** PREPENDING ****
[Seq] - 16382 nanoseconds
[List] - 406 nanoseconds
[Vector] - 4374 nanoseconds
****** 2^8 elements *******
**** APPLY ****
[Seq] - 868 nanoseconds
[List] - 9936 nanoseconds
[Vector] - 543 nanoseconds
**** APPENDING ****
[Seq] - 26681 nanoseconds
[List] - 10397 nanoseconds
[Vector] - 5389 nanoseconds
**** PREPENDING ****
[Seq] - 28092 nanoseconds
[List] - 468 nanoseconds
[Vector] - 4293 nanoseconds
****** 2^9 elements *******
**** APPLY ****
[Seq] - 989 nanoseconds
[List] - 5869 nanoseconds
[Vector] - 518 nanoseconds
**** APPENDING ****
[Seq] - 37614 nanoseconds
[List] - 19990 nanoseconds
[Vector] - 4240 nanoseconds
**** PREPENDING ****
[Seq] - 37230 nanoseconds
[List] - 447 nanoseconds
[Vector] - 4446 nanoseconds
****** 2^10 elements *******
**** APPLY ****
[Seq] - 941 nanoseconds
[List] - 21657 nanoseconds
[Vector] - 2065 nanoseconds
**** APPENDING ****
[Seq] - 74518 nanoseconds
[List] - 37636 nanoseconds
[Vector] - 8746 nanoseconds
**** PREPENDING ****
[Seq] - 79404 nanoseconds
[List] - 495 nanoseconds
[Vector] - 8403 nanoseconds
****** 2^11 elements *******
**** APPLY ****
[Seq] - 1012 nanoseconds
[List] - 71802 nanoseconds
[Vector] - 642 nanoseconds
**** APPENDING ****
[Seq] - 223876 nanoseconds
[List] - 78689 nanoseconds
[Vector] - 7727 nanoseconds
**** PREPENDING ****
[Seq] - 173258 nanoseconds
[List] - 509 nanoseconds
[Vector] - 5434 nanoseconds
****** 2^12 elements *******
**** APPLY ****
[Seq] - 931 nanoseconds
[List] - 114779 nanoseconds
[Vector] - 2842 nanoseconds
**** APPENDING ****
[Seq] - 369361 nanoseconds
[List] - 168447 nanoseconds
[Vector] - 7570 nanoseconds
**** PREPENDING ****
[Seq] - 392323 nanoseconds
[List] - 1242 nanoseconds
[Vector] - 10431 nanoseconds
****** 2^13 elements *******
**** APPLY ****
[Seq] - 1609 nanoseconds
[List] - 75484 nanoseconds
[Vector] - 748 nanoseconds
**** APPENDING ****
[Seq] - 386051 nanoseconds
[List] - 442218 nanoseconds
[Vector] - 3904 nanoseconds
**** PREPENDING ****
[Seq] - 265421 nanoseconds
[List] - 544 nanoseconds
[Vector] - 5109 nanoseconds
****** 2^14 elements *******
**** APPLY ****
[Seq] - 1895 nanoseconds
[List] - 32114 nanoseconds
[Vector] - 1360 nanoseconds
**** APPENDING ****
[Seq] - 722224 nanoseconds
[List] - 3366607 nanoseconds
[Vector] - 16948 nanoseconds
**** PREPENDING ****
[Seq] - 908186 nanoseconds
[List] - 1183 nanoseconds
[Vector] - 11446 nanoseconds
****** 2^15 elements *******
**** APPLY ****
[Seq] - 1036 nanoseconds
[List] - 151284 nanoseconds
[Vector] - 2513 nanoseconds
**** APPENDING ****
[Seq] - 716991 nanoseconds
[List] - 3672804 nanoseconds
[Vector] - 9737 nanoseconds
**** PREPENDING ****
[Seq] - 656793 nanoseconds
[List] - 1993 nanoseconds
[Vector] - 7341 nanoseconds
****** 2^16 elements *******
**** APPLY ****
[Seq] - 857 nanoseconds
[List] - 158413 nanoseconds
[Vector] - 608 nanoseconds
**** APPENDING ****
[Seq] - 7693809 nanoseconds
[List] - 1649853 nanoseconds
[Vector] - 5268 nanoseconds
**** PREPENDING ****
[Seq] - 5987314 nanoseconds
[List] - 2519 nanoseconds
[Vector] - 7531 nanoseconds
****** 2^17 elements *******
**** APPLY ****
[Seq] - 927 nanoseconds
[List] - 38256 nanoseconds
[Vector] - 649 nanoseconds
**** APPENDING ****
[Seq] - 10040950 nanoseconds
[List] - 8400181 nanoseconds
[Vector] - 3978 nanoseconds
**** PREPENDING ****
[Seq] - 41605877 nanoseconds
[List] - 415 nanoseconds
[Vector] - 4975 nanoseconds
****** 2^18 elements *******
**** APPLY ****
[Seq] - 891 nanoseconds
[List] - 366869 nanoseconds
[Vector] - 2546 nanoseconds
**** APPENDING ****
[Seq] - 18920417 nanoseconds
[List] - 55552109 nanoseconds
[Vector] - 9095 nanoseconds
**** PREPENDING ****
[Seq] - 9705251 nanoseconds
[List] - 345 nanoseconds
[Vector] - 5363 nanoseconds
****** 2^19 elements *******
**** APPLY ****
[Seq] - 1118 nanoseconds
[List] - 5441716 nanoseconds
[Vector] - 703 nanoseconds
**** APPENDING ****
[Seq] - 90234084 nanoseconds
[List] - 19461400 nanoseconds
[Vector] - 8432 nanoseconds
**** PREPENDING ****
[Seq] - 10769801 nanoseconds
[List] - 385 nanoseconds
[Vector] - 10124 nanoseconds
****** 2^20 elements *******
**** APPLY ****
[Seq] - 877 nanoseconds
[List] - 3018984 nanoseconds
[Vector] - 462 nanoseconds
**** APPENDING ****
[Seq] - 155539391 nanoseconds
[List] - 117819250 nanoseconds
[Vector] - 8754 nanoseconds
**** PREPENDING ****
[Seq] - 23674057 nanoseconds
[List] - 294 nanoseconds
[Vector] - 7160 nanoseconds
****** 2^21 elements *******
**** APPLY ****
[Seq] - 1129 nanoseconds
[List] - 2029575 nanoseconds
[Vector] - 531 nanoseconds
**** APPENDING ****
[Seq] - 329530744 nanoseconds
[List] - 107750515 nanoseconds
[Vector] - 3395 nanoseconds
**** PREPENDING ****
[Seq] - 47478839 nanoseconds
[List] - 306 nanoseconds
[Vector] - 4057 nanoseconds
****** 2^22 elements *******
**** APPLY ****
[Seq] - 990 nanoseconds
[List] - 28561618 nanoseconds
[Vector] - 1407 nanoseconds
**** APPENDING ****
[Seq] - 799933624 nanoseconds
[List] - 253773273 nanoseconds
[Vector] - 2723 nanoseconds
**** PREPENDING ****
[Seq] - 101601837 nanoseconds
[List] - 439 nanoseconds
[Vector] - 6584 nanoseconds
****** 2^23 elements *******
**** APPLY ****
[Seq] - 930 nanoseconds
[List] - 67502628 nanoseconds
[Vector] - 627 nanoseconds
**** APPENDING ****
[Seq] - 1298660621 nanoseconds
[List] - 1105467388 nanoseconds
[Vector] - 5437 nanoseconds
**** PREPENDING ****
[Seq] - 269569362 nanoseconds
[List] - 327 nanoseconds
[Vector] - 5480 nanoseconds
****** 2^24 elements *******
**** APPLY ****
[Seq] - 963 nanoseconds
[List] - 45554307 nanoseconds
[Vector] - 563 nanoseconds
**** APPENDING ****
[Seq] - 5079547561 nanoseconds
[List] - 1693579905 nanoseconds
[Vector] - 4396 nanoseconds
**** PREPENDING ****
[Seq] - 499580053 nanoseconds
[List] - 297 nanoseconds
[Vector] - 7645 nanoseconds
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment