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

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 :
SetMap(默认由Hash表实现)用contains查找是最快的,因为它们计算hash值然后立即跳到正确的位置。例如,如果你想从一个1000项的list中找一个任意字符串,用containsSet上查找比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.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值