当前位置:网站首页>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.)
边栏推荐
- 一年130+新服务和功能,这个存储“全家桶”又壮大了
- Redis——JedisConnectionException Could not get a resource from the pool
- 局域网SDN技术硬核内幕 4 从计算虚拟化到网络虚拟化
- About redis, do you update the database first or the cache first?
- 局域网SDN技术硬核内幕 6 分布式任意播网关
- Problems encountered in punching
- 如何使用订单流分析工具(下)
- Application of workflow engine in vivo marketing automation
- 如何使用订单流分析工具(中)
- 升级poi-tl版本1.12.0与旧版poi(4.1.2)、easyexcel之间的依赖冲突解决
猜你喜欢

Uniapp switches the tab bar to display different pages, remembers the page location and pulls up to get new data

002_Kubernetes安装配置

Talk about repaintboundary in fluent
![[technology popularization] alliance chain layer2- on a new possibility](/img/e1/be9779eee3d3d4dcf56e103ba1d3d6.jpg)
[technology popularization] alliance chain layer2- on a new possibility

Redis三种集群方案

类和对象(1)

基于ROS的导航框架

Wechat campus second-hand book trading applet graduation design finished product (7) Interim inspection report

Wechat campus second-hand book trading applet graduation design finished product (6) opening defense ppt

I use the factory mode in jd.com and explain the factory mode clearly
随机推荐
7. Learn Mysql to select a database
LAN SDN technology hard core insider 4 from computing virtualization to network virtualization
【翻译】宣布Krius--加速你对Kubernetes的监控采用
文本攻击方法开源代码汇总
成功解决:error: src refspec master does not match any
JS determines the scrolling element and solves the tab to switch the scrolling position independently
类和对象(1)
(五)数电——公式化简法
Report on the progress of writing Chinese experiments by latex
[technology popularization] alliance chain layer2- on a new possibility
多传感器融合综述---FOV与BEV
Uniapp switches the tab bar to display different pages, remembers the page location and pulls up to get new data
局域网SDN技术硬核内幕 7 从二层到大二层
Alibaba Cloud Security Center's best practices for vulnerability repair
如何优雅的改变this指向
局域网SDN硬核技术内幕 19 团结一切可以团结的力量
Leetcode 757 set the intersection size to at least 2[sort greedy] the leetcode path of heroding
Wechat campus second-hand book trading applet graduation design finished product (4) opening report
With 130 new services and functions a year, this storage "family bucket" has grown again
微信小程序项目实战