Reputation: 4643
I had this code:
:history
(cons [t (:latest thing)] (take n (:history thing)) )
which was for adding a rolling window of recent history to a map on each iteration of my program. What I found was that after a certain number of iterations (~50) my program would slow down dramatically and progressively.
whereas, if I changed the code to:
:history
(cons [t (:latest thing)] (take n (vec (:history thing))) )
then the program ran slightly longer on each iteration (as the lazy seq was being realized), but ran consistently and did not slow down.
Being new to Clojure I don't understand...is it to do with chunked sequences ?
Upvotes: 0
Views: 89
Reputation: 33637
I think by program slowdown you mean to say "the consumption of this lazy sequence slows down as the sequence becomes bigger due to may cons operations that your first code sample does". This is because when you build a lazy sequence using lazy operators like cons, it creates a chain of operations (functions) to generate the sequence and this chain of operation will gets executed every time you consume this lazy sequence for ex: 50 operations of cons will create a 50 chained function calls, which gets executed every time you consume the sequence which will obviously be slow than having a vector of 50 elements and consuming it.
In your second case the lazy sequence will have only 1 operation (i.e the cons) and rest will be take operation from a already realized vector (i.e the vector call)
Upvotes: 1