Actaully caching makes a difference even with inc and a sequence of just
1000 elements:

Clojure 1.7.0-alpha5
user=> (def a (iterate inc 0))
#'user/a
user=> (time (reduce (fn [_ x]) nil (take 1000 a)))
"Elapsed time: 4.170778 msecs"
nil
user=> (time (reduce (fn [_ x]) nil (take 1000 a)))
"Elapsed time: 1.589569 msecs"
nil

Clojure 1.7.0-master-SNAPSHOT
user=> (def a (iterate inc 0))
#'user/a
user=> (time (reduce (fn [_ x]) nil (take 1000 a)))
"Elapsed time: 4.831726 msecs"
nil
user=> (time (reduce (fn [_ x]) nil (take 1000 a)))
"Elapsed time: 4.058695 msecs"
nil

Nicola Mometto writes:

> The recent changes to iterate come with an interesting consequence:
> reducing an iterate multiple times will cause the entire chain of x, (f
> x), (f (f x)) .. to be recalculated every time.
>
> I'd argue that this is not desiderable and a regression (even though
> probably one considered by design), and that this change in behaviour is
> going to produce a degradation in performance when the function to
> iterate actually does some computation rather than being a trivial
> function like the one used in the benchmarks in the CLJ-1603 ticket
> (inc) since its result won't be cached.
>
> If changing the new behaviour is out of the question I'd suggest to at
> least document this new behaviour. The docstring of iterate talks about
> returning a lazy-seq while when used with reduce it is actually a
> generator.
>
> To show what I mean here's a silly example using Threa/sleep to simulate
> computation:
>
> Clojure 1.7.0-alpha5
> user=> (def a (iterate #(do (Thread/sleep 2) (inc %)) 0))
> #'user/a
> user=> (time (reduce (fn [_ x] (if (= 50 x) (reduced nil))) nil a))
> "Elapsed time: 106.385891 msecs"
> nil
> user=> (time (reduce (fn [_ x] (if (= 50 x) (reduced nil))) nil a))
> "Elapsed time: 0.560275 msecs"
> nil
>
> Clojure 1.7.0-master-SNAPSHOT
> user=> (def a (iterate #(do (Thread/sleep 2) (inc %)) 0))
> #'user/a
> user=> (time (reduce (fn [_ x] (if (= 50 x) (reduced nil))) nil a))
> "Elapsed time: 109.088477 msecs"
> nil
> user=> (time (reduce (fn [_ x] (if (= 50 x) (reduced nil))) nil a))
> "Elapsed time: 109.51494 msecs"
> nil

--

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to