您的位置:首页 > 大数据 > 人工智能

Scala 当用到.contains() .exists()的性能问题

2015-12-21 13:26 1166 查看
SCALA: Which data structures are optimal in which siutations when using “.contains()” or “.exists()”?

Q: 当用到”.contains()”或”.exists()”时,哪种数据结构的性能是最好的?

比如:

val m=Map(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4)
// m: scala.collection.immutable.Map[Int,Int]= Map(1 -> 1, 2 -> 2, 3 -> 3, 4-> 4)

val l = List(1,2,3,4)
// l:List[Int] = List(1,2,3,4)

val v = Vector(1,2,3,4)
// v: scala.collection.immutable.Vector[Int] = Vector(1,2,3,4)

m.exists(_._1 == 3)                       //> res0: Boolean = true
m.contains(3)                             //> res1: Boolean = true
l.exists(_ == 3)                          //> res2: Boolean = true
l.contains(3)                             //> res3: Boolean = true
v.exists(_ == 3)                          //> res4: Boolean = true
v.contains(3)                             //> res5: Boolean = true


直觉上,我认为向量应该是最快的随机检查,如果有一个知道数值应该是在开头检查的话,列表是最快的。此外,请扩展到其他数据结构。如果你觉得这个问题太模糊,请让我知道。我不确定我的语法是否正确。

A :

Set
Map
(默认由Hash表实现)用contains查找是最快的,因为它们计算hash值然后立即跳到正确的位置。例如,如果你想从一个1000项的list中找一个任意字符串,用
contains
Set
上查找比
List,Vector或Array
快大约100倍。

当使用
exists()
时,你只要关心的是: how fast the collection is to traverse——集合遍历的速度,因为你必须traverse everything anyway. 这时候,
List
通常最好,除非你手动遍历
Array
.但
Set
是特别差的,如:在List上使用exists比Set上快大约8倍,如果都是1000个元素的话。其他的结构花的时间大概是
List
的2.5x(通常是1.5x,但是Vector有一个基本的树形结构,快速遍历起来不是那么快??which is not all that fast to traverse.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: