On Tue, Mar 3, 2009 at 2:54 PM, Mark Volkmann <r.mark.volkm...@gmail.com> wrote:
>
> I agree that using doseq instead of dorun results in code that is
> easier to read. I don't understand though why it is more efficient.
> They both walk the entire sequence, neither holds onto the head and
> both return nil. Why is doseq more efficient than dorun?

A simple doseq does run about as fast as dorun on the same seq.  Bus
something like this would be more likely (let's pretend inc has useful
side-effects):

  (defn f [] (dorun (map #(inc %) (range 10000000))))

  (defn f2 [] (doseq [i (range 10000000)] (inc i)))

The results would be the same, but dorun requires 'map', which creates
another seq.  'doseq' is able to accomplish the same work with less
allocation.

  user=> (time (f))
  "Elapsed time: 1685.796066 msecs"

  user=> (time (f2))
  "Elapsed time: 531.730209 msecs"

--Chouser

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