performance - SCALA: Which data structures are optimal in which siutations when using ".contains()" or ".exists()"? -
i know in situations data structures optimal using "contains" or "exists" checks.
i ask because come python background , used using if x in something:
expressions everything. example, expressions evaluated quickest:
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
intuitively, assume vectors should quickest random checks, , lists quickest if 1 knows value checked in beginning of list , there lot of data. however, confirmation or correction welcome. also, please feel free expand other data structures.
note: please let me know if feel question vague i'm not sure phrasing correctly.
set
, map
(with default hash table implementation) far fastest @ contains
since compute hash value , jump right location immediately. example, if want find arbitrary string out of list of thousand, contains
on set 100x faster contains
on list
or vector
or array
.
with exists
, care how fast collection traverse--you have traverse anyway. there, list
champ (unless want traverse array hand), set
, on particularly bad (e.g. exists
on list
~8x faster on set
when each have 1000 elements). others within 2.5x of list
(usually 1.5x, vector
has underlying tree structure not fast traverse).
Comments
Post a Comment