Here are some potentially interesting observations.  First, as similar
lazy and eager versions as I could come up with:

(defn flatten-maps-lazy [coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (let [m (first s)]
        (cons (dissoc m :c)
              (flatten-maps-lazy
                (concat (get m :c) (rest s))))))))

(defn flatten-maps-eager [coll]
  (loop [r nil s coll]
    (if-let [s (seq s)]
      (let [m (first s)]
        (recur
          (cons (dissoc m :c) r)
          (concat (get m :c) (rest s))))
      r)))

(defonce d (data 200))


Some results:

user> (dotimes [_ 10] (time (let [s (flatten-maps-lazy bd)] (println
[(first s) (apply + (map (comp count keys) s))]))))
...
"Elapsed time: 751.97 msecs"

user> (dotimes [_ 10] (time (let [s (flatten-maps-lazy bd)] (println
[(apply + (map (comp count keys) s)) (first s)]))))
...
"Elapsed time: 1211.047 msecs"

user> (dotimes [_ 10] (time (let [s (flatten-maps-eager bd)] (println
[(first s) (apply + (map (comp count keys) s))]))))
...
"Elapsed time: 986.66 msecs"

user> (dotimes [_ 10] (time (let [s (flatten-maps-eager bd)] (println
[(apply + (map (comp count keys) s)) (first s)]))))
...
"Elapsed time: 993.345 msecs"

So, the lazy version is faster when the compiler can do fine-grained
locals clearing, because then presumably the JIT figures out that the
sequence never has to be actually constructed in memory (??).  The
eager version is faster when you actually need the whole sequence
there.  The speed of the lazy version may also be partially due to
better memory locality.

I don't know if this actually explains your results, since I don't
know what the JIT can conclude about the result of your doall.  I
generally prefer something like the above, where it's clear exactly
how the output of the profiled function is consumed (and thus, what
code cannot be considered dead and eliminated by the JIT).

-Jason

-- 
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

Reply via email to