performance - Conversion between different implementations of Seq in Scala -
from have read, 1 should prefer use generic seq when defining sequences instead of specific implementations such list
or vector
.
though have parts of code when sequence used full traversal (mapping, filtering, etc) , parts of code same sequence used indexing operations (indexof, lastindexwhere).
in first case, think better use linearseq
(implem list
) whereas in second case better use indexedseq
(implem vector
).
my question is: need explicitly call conversion method tolist
, toindexedseq
in code or conversion done under hood in intelligent manner ? if use these conversions, penalty performance when going , forth between indexedseq
, linearseq
?
thanks in advance
vector
out-perform list
. unless algorithm uses ::
, head
, tail
, vector
faster list
. using list
more of conceptual question on algorithm (data stack-structured, accessing head/tail, adding elements prepending, use of pattern matching (which can used vector
, feels more natural me use list
)).
you might want @ why should use vector in scala
now nice number compare (obviously not 'real' benchmark, eh) :
val l = list.range(1,1000000) val = vector.range(1,1000000) import system.{currenttimemillis=> milli} val startlist = milli l.map(_*2).map(_+2).filter(_%2 == 0) println(s"time list map/filter operations : ${milli - startlist}") val startvector = milli a.map(_*2).map(_+2).filter(_%2 == 0) println(s"time vector map/filter operations : ${milli - startvector}")
output :
time list map/filter operations : 1214 time vector map/filter operations : 364
edit : realized doesn't answer question. far know, have call tolist/tovector yourself. performances, depends on sequence, unless you're going , forth time, shouldn't problem.
once again, not serious benchmark, :
val startconvtolist = milli a.tolist println(s"time conversion list: ${milli - startconvtolist}") val startconvtovector = milli l.tovector println(s"time conversion vector: ${milli - startconvtovector}")
output :
time conversion list: 48 time conversion vector: 18
Comments
Post a Comment