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()”时,哪种数据结构的性能是最好的?
比如:
直觉上,我认为向量应该是最快的随机检查,如果有一个知道数值应该是在开头检查的话,列表是最快的。此外,请扩展到其他数据结构。如果你觉得这个问题太模糊,请让我知道。我不确定我的语法是否正确。
A :
当使用
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.)
相关文章推荐
- 'library cache lock' Waits: Causes and Solutions
- Jenkins进阶系列之——02email-ext邮件通知模板
- 生产者与消费者模式-阻塞 wait,notify
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- 内存详解--理解 JVM 如何使用 AIX 上的本机内存
- Fix “Could not flush the DNS Resolver Cache: Function failed during execution” When Flushing DNS
- freemarker报错:Tip: If the failing expression is known to be legally null/missing, either specify a de
- sleep和wait的区别
- 第四十三篇:haartrainning结果的含义
- XPath语法 在C#中使用XPath示例 【转http://www.cnblogs.com/yukaizhao/archive/2011/07/25/xpath.html】非常详细的文章
- 关于Keychain Sharing的一些个人经验
- yum安装时出现:Cannot retrieve metalink for repository: epel. Please verify its path and try again
- hdu 5029 Relief grain(树链剖分+线段树)
- Tracking down the Villains: Outlier Detection at Netflix
- CF#335 Sorting Railway Cars
- List中的Contains
- Rails(UVa514)(栈)
- Daily Scrumming* 2015.12.20(Day 12)
- 基于积分系统的AI设计
- 1008. Airline Routes (35)