Clearly Seq
asymptotically performs the same or better as []
for all possible operations. But since its structure is more complicated than lists, for small sizes its constant overhead will probably make it slower. I'd like to know how much, in particular:
- How much slower is
<|
compared to:
? - How much slower is folding over/traversing
Seq
compared to folding over/traversing[]
(excluding the cost of a folding/traversing function)? - What is the size (approximately) for which
\xs x -> xs ++ [x]
becomes slower than|>
? - What is the size (approximately) for which
++
becomes slower than><
? - What's the cost of calling
viewl
and pattern matching on the result compared to pattern matching on a list? - How much memory does an
n
-elementSeq
occupy compared to ann
-element list? (Not counting the memory occupied by the elements, only the structure.)
I know that it's difficult to measure, since with Seq
we talk about amortized complexity, but I'd like to know at least some rough numbers.
I have one more concrete result to add to above answer. I am solving a Langevin equation. I used List and Data.Sequence. A lot of insertions at back of list/sequence are going on in this solution.
To sum up, I did not see any improvement in speed, in fact performance deteriorated with Sequences. Moreover with Data.Sequence, I need to increase the memory available for Haskell RTS.
Since I am definitely not an authority on optimizing; I post the both cases below. I'd be glad to know if this can be improved. Both codes were compiled with
-O2
flag.