Skip to content

Instantly share code, notes, and snippets.

@notyy
Last active August 29, 2015 14:03
Show Gist options
  • Save notyy/7f3c211e69be4aabb43a to your computer and use it in GitHub Desktop.
Save notyy/7f3c211e69be4aabb43a to your computer and use it in GitHub Desktop.
package microbenchmarks
import org.scalameter.PerformanceTest
import org.scalameter.api._
import scala.collection.mutable.ArrayBuffer
object CollectionAppendBenchmark extends PerformanceTest.OfflineReport {
val sizes: Gen[Int] = Gen.range("size")(100, 500, 100)
var list = List.empty[Int]
var array = ArrayBuffer.empty[Int]
var vector = Vector.empty[Int]
performance of "Vector" in {
measure method ":+" in {
using(sizes) in { s =>
var max = 0
while(max < s) {
vector = vector :+ 1
max += 1
}
}
}
}
performance of "List" in {
// measure method ":+" in {
// using(sizes) in { s =>
// var max = 0
// while(max < s) {
// list = list :+ 1
// max += 1
// }
// }
// }
measure method ":: then reverse" in {
using(sizes) in { s =>
var max = 0
while(max < s) {
list = 1 :: list
max += 1
}
}
list.reverse
}
}
performance of "ArrayBuffer" in {
measure method ":+" in {
using(sizes) in { s =>
var max = 0
while(max < s) {
array = array :+ 1
max += 1
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment