Skip to content

Instantly share code, notes, and snippets.

@amaya382
Last active August 29, 2015 14:11
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 amaya382/86d8a74af5ae697ae341 to your computer and use it in GitHub Desktop.
Save amaya382/86d8a74af5ae697ae341 to your computer and use it in GitHub Desktop.
immutable なコレクション(immutable.List)と mutable なコレクションの(mutable.ListBuffer) append 性能比較
def append2Immutable(noTimes: Long): Unit = {
@scala.annotation.tailrec
def go(l: List[Int], i: Long = 0): List[Int] = {
if (i > noTimes) l
else go(l :+ 0, i + 1)
}
go(List())
}
def append2Mutable(noTimes: Long): Unit = {
import scala.collection.mutable.ListBuffer
@scala.annotation.tailrec
def go(l: ListBuffer[Int], i: Long = 0): ListBuffer[Int] = {
if (i > noTimes) l
else go(l += 0, i + 1)
}
go(ListBuffer())
}
val result4Immutable = (1 to 10000 by 100).map(i => runBenchmark(append2Immutable(i), 10))
val result4Mutable = (1 to 10000 by 100).map(i => runBenchmark(append2Mutable(i), 10))
scala> result4Immutable.foreach(println)
21.2
244.0
758.9
1613.5
3066.0
4669.0
6024.1
9134.7
11272.8
16305.5
19996.2
24145.9
27714.0
33141.3
38865.4
40846.7
46562.0
52854.0
68272.5
67103.1
77891.7
78636.4
85983.3
102651.2
108823.9
113040.7
127835.7
134558.4
140059.5
152845.4
167230.5
175332.5
183521.1
191862.4
207307.9
223187.1
237266.8
284214.4
279661.8
290782.7
287950.3
297723.5
323606.3
330136.8
348516.6
368237.8
366190.5
399862.4
420186.1
419141.1
446623.9
488527.7
542659.3
503791.3
518494.5
543854.9
570079.7
574571.9
609353.5
612181.2
639447.5
657675.0
693544.3
703704.4
730827.1
745481.2
781298.3
800952.4
825311.1
899367.0
885150.4
904822.2
935736.9
950342.5
969883.5
986695.7
1050972.7
1054393.8
1081256.8
1099920.1
1138299.8
1164651.3
1196613.0
1218319.3
1265369.6
1307168.7
1327700.3
1348182.4
1397775.0
1407217.0
1460849.7
1485046.1
1510593.2
1601664.0
1667945.1
1725163.0
1714159.4
1731459.7
1719993.6
1727118.6
scala> result4Mutable.foreach(println)
20.1
27.6
33.4
37.3
45.1
71.0
84.8
76.2
24.1
26.5
22.0
23.5
26.0
23.7
25.3
26.4
26.5
25.2
26.4
34.4
27.0
28.5
26.9
30.0
33.3
32.6
31.4
27.9
32.2
36.3
32.0
34.8
36.1
31.6
36.3
36.5
33.9
36.0
41.5
35.2
33.4
37.8
51.5
48.6
55.5
47.8
38.0
46.5
45.4
41.8
49.3
44.7
44.3
46.6
51.9
61.6
63.1
60.4
60.0
59.9
64.4
55.5
62.6
53.3
46.8
47.5
54.1
47.5
53.5
51.6
50.9
52.7
57.0
58.1
50.8
58.5
54.2
59.6
71.5
55.0
52.4
59.3
69.1
64.3
86.1
83.3
60.7
62.0
63.5
78.0
60.1
62.3
63.6
65.1
67.1
65.2
69.9
67.5
86.9
70.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment