当前位置:网站首页>Scala 当用到.contains() .exists()的性能问题
Scala 当用到.contains() .exists()的性能问题
2022-07-22 21:23:00 【武念】
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.)
边栏推荐
- 文本攻击方法开源代码汇总
- Z-Wave 800:SE 固件升级
- Draw a wave ball with the curve of flutter and Bessel
- Wechat hotel reservation applet graduation project (5) assignment
- Wechat hotel reservation applet graduation project (8) graduation project thesis template
- LAN SDN hard core technology insider 18 beautiful new world
- Report on the progress of writing Chinese experiments by latex
- 成功解决:error: src refspec master does not match any
- Codeforces Round #808 (Div. 2) A - D
- 我是如何在一周内拿到4份offer的?
猜你喜欢

沃尔沃xc90的安全性如何?一起来看看吧

I use the factory mode in jd.com and explain the factory mode clearly

Copytexture, copytoresolvetarget of UE4 engine

Small program completion work wechat campus second-hand book trading small program graduation design finished product (2) small program function

2022 employment season surprise! The genuine Adobe software can finally be used for nothing

LeetCode(剑指 Offer)- 11. 旋转数组的最小数字

2022就业季惊喜来袭!正版Adobe软件,终于能正经白嫖一把了

93. (leaflet chapter) leaflet situation plotting - modification of attack direction

第一篇sourcetree安装

93.(leaflet篇)leaflet态势标绘-进攻方向修改
随机推荐
局域网SDN硬核技术内幕 18 美丽新世界
VScode配置用户代码片段
[untitled] share an API Gateway project developed based on ABP vNext
Wechat campus second-hand book trading applet graduation design finished product (4) opening report
Talk about repaintboundary in fluent
How to use the order flow analysis tool (in)
GNU LD script command language (II)
Understanding service governance in distributed development
7. Learn Mysql to select a database
【刷题记录】18. 四数之和
升级poi-tl版本1.12.0与旧版poi(4.1.2)、easyexcel之间的依赖冲突解决
LAN SDN technology hard core insider 6 distributed anycast gateway
Utools recommendation
Wechat hotel reservation applet graduation project (8) graduation project thesis template
JS determines the scrolling element and solves the tab to switch the scrolling position independently
Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
2022 employment season surprise! The genuine Adobe software can finally be used for nothing
类和对象(1)
Ftxui basic notes (Hello World)
Basic commands of redis' five basic data types