On Mon, Dec 29, 2008 at 9:36 PM, Aaron Brooks <aa...@brooks1.net> wrote:
>
> In Clojure (anybody correct me if I'm wrong) I think it's preferable
> for performance reasons to use reduce instead of apply when you can.

I actually think that's backwards. In many cases it doesn't really
matter. It's common to do what + currently does, and that is call
reduce internally if given more than 2 arguments.  However, it's
possible for a function to do something more clever when called via
apply, because it controls any looping itself.  The 'str' function,
for example, uses a single StringBuilder internally when given more
than 1 argument, so it's much more efficient to call (apply str foo)
than (reduce str foo) for large collections.

user=> (time (last (reduce str (range 30000))))
"Elapsed time: 5570.028441 msecs"
\9
user=> (time (last (apply str (range 30000))))
"Elapsed time: 54.140901 msecs"
\9

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