str is much faster with apply than with reduce; in fact, with reduce
it is O(n^2), whereas with apply it is O(n). (The constants are better
with apply too.)

In general, (apply foo ...) takes advantage of any optimizations the
author of foo might be aware of for speeding up its operation when
given multiple arguments. (reduce foo ...) is always a simple-minded
fold. Not that that's never useful; it is with reducers and with
anonymous functions.


On 12 February 2014 21:06, Vincent <vhenneb...@gmail.com> wrote:
> On Wednesday, February 12, 2014 2:47:07 PM UTC, Stuart Sierra wrote:
>>
>> On Wednesday, February 12, 2014 8:46:41 AM UTC-5, Vincent wrote:
>>>
>>> On a slightly different topic: why reduce and not apply?
>>
>>
>> The implementation of `+` with more than 2 arguments uses `reduce`
>> internally. So they amount to the same thing.
>
>
> Ah ok, interesting. I do seem to have, in the past, come across a function
> that was significantly faster when used with apply rather than reduce, but I
> can't remember which one.
>
>
>>
>> There isn't really a performance difference:
>>
>> user=> (dotimes [i 7] (time (reduce + (range 10000000))))
>> "Elapsed time: 354.475 msecs"
>> "Elapsed time: 346.235 msecs"
>> "Elapsed time: 348.124 msecs"
>> "Elapsed time: 348.894 msecs"
>> "Elapsed time: 379.9 msecs"
>> "Elapsed time: 356.337 msecs"
>> "Elapsed time: 362.788 msecs"
>> nil
>> user=> (dotimes [i 7] (time (apply + (range 10000000))))
>> "Elapsed time: 360.067 msecs"
>> "Elapsed time: 353.281 msecs"
>> "Elapsed time: 345.694 msecs"
>> "Elapsed time: 355.162 msecs"
>> "Elapsed time: 346.511 msecs"
>> "Elapsed time: 350.61 msecs"
>> "Elapsed time: 353.674 msecs"
>> nil
>
>
> Thanks,
> Vincent
>
> --
> 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/groups/opt_out.

-- 
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/groups/opt_out.

Reply via email to