def main(args: Array[String]): Unit = {
val t3 = System.currentTimeMillis()
val key = "a"
for (j <- 0 to 100000) {
var expData : scala.collection.mutable.Map[String, ArrayBuffer[Double]] = scala.collection.mutable.Map("a" -> ArrayBuffer[Double](1))
for (i <- 0 to 10000) {
if (expData.contains(key)) {
expData(key) += i.toDouble
} else {
expData(key) = ArrayBuffer(i.toDouble)
}
}
}
val t4 = System.currentTimeMillis()
println("expData 2 time : 执行" + (t4 - t3)/1000 + "秒" )
val t5 = System.currentTimeMillis()
val key2 = "a"
for (j <- 0 to 100000) {
var expData : scala.collection.mutable.Map[String, ArrayBuffer[Double]] = scala.collection.mutable.Map("a" -> ArrayBuffer[Double](1))
for (i <- 0 to 10000) {
expData.put(key2,expData.getOrElse(key2, ArrayBuffer[Double]()) += i.toDouble)
}
}
val t6 = System.currentTimeMillis()
println("expData 3 time : 执行" + (t6 - t5)/1000 + "秒" )
}
如图,更新map数据,分别使用两种方式(expData(key)、put|get)
1. expData(key) += i.toDouble
2. expData.put(key2,expData.getOrElse(key2, ArrayBuffer[Double]()) += i.toDouble)
处理了100000*10000=10亿次
结果如下:
expData(key)的执行32秒,put|get执行139秒
结论,expData(key)的速度是put|get的4倍多