0

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 ?

Hendekagon
  • 4,565
  • 2
  • 28
  • 43

1 Answers1

1

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)

Ankur
  • 33,367
  • 2
  • 46
  • 72
  • ok, I can see why the first case is bad, but why the slowdown only when it hits a certain number of iterations ? is that due to chucked sequences ? – Hendekagon Apr 13 '12 at 06:00
  • actually, now I'm confused, is the first case bad ? do all those nested cons operations get executed every time I consume the sequence ? Surely since I take n each time, then the sequence only contains those n items. It *is* bad in another way, which is that it's a wasteful way to do it anyway - I should be taking the rest and conjing or something so I don't need those n iterations for take. – Hendekagon Apr 13 '12 at 07:08