1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
object TreeSetInsertRandom {
def main(args: Array[String]): Unit = {
val n = 500000
new JavaUtilTS(n).main(args)
new MutableTS(n).main(args)
new ImmutableTS(n).main(args)
}
}
class Dummy(val a: Int) extends math.Ordered[Dummy] {
def compare(other: Dummy) = this.a - other.a
override def toString = a.toString
}
class JavaUtilTS(val length: Int) extends testing.Benchmark {
var data: Array[Dummy] = util.Random.shuffle((0 until length) map { a => new Dummy(a) }) toArray
var t: java.util.TreeSet[Dummy] = null
def run = {
t = new java.util.TreeSet[Dummy]()
var i = 0
while (i < length) {
val elem = data(i)
t add elem
i += 1
}
}
}
class MutableTS(val length: Int) extends testing.Benchmark {
var data: Array[Dummy] = util.Random.shuffle((0 until length) map { a => new Dummy(a) }) toArray
var t: collection.mutable.TreeSet[Dummy] = null
def run = {
t = collection.mutable.TreeSet[Dummy]()
var i = 0
while (i < length) {
val elem = data(i)
t += elem
i += 1
}
}
}
class ImmutableTS(val length: Int) extends testing.Benchmark {
var data: Array[Dummy] = util.Random.shuffle((0 until length) map { a => new Dummy(a) }) toArray
var t: collection.immutable.TreeSet[Dummy] = null
def run = {
t = collection.immutable.TreeSet[Dummy]()
var i = 0
while (i < length) {
val elem = data(i)
t += elem
i += 1
}
}
}
|