On Tue, Jun 23, 2009 at 8:02 PM, Daniel Lyons <fus...@storytotell.org>wrote:
> > > On Jun 23, 2009, at 11:37 AM, Kyle Schaffrick wrote: > > > As an aside, I also notice you prefer 'reduce to 'apply when using > > arithmetic functions, yet I've seen both in the wild. I'm just > > guessing > > you prefer to make it explicit that you're doing a reduction, but I > > wonder if one is better than another? > > > That's an interesting question. I just benchmarked 'em and found this: > > user> (time (reduce + (range 100000000))) > "Elapsed time: 4724.192 msecs" > 4999999950000000 > > user> (time (apply + (range 100000000))) > "Elapsed time: 3018.139 msecs" > 4999999950000000 Hmm, no. apply should be slower since the var-arg body for + use reduce: http://github.com/richhickey/clojure/blob/b03e19aa341fea01c1279a74f4184f6538d0f72e/src/clj/clojure/core.clj#L556 user=> (dotimes [_ 10] (time (apply + (range 100000000)))) "Elapsed time: 2607.452135 msecs" "Elapsed time: 2622.394746 msecs" "Elapsed time: 2643.195364 msecs" "Elapsed time: 2641.422793 msecs" "Elapsed time: 2642.319066 msecs" "Elapsed time: 2566.04743 msecs" "Elapsed time: 2566.192631 msecs" "Elapsed time: 2570.2414 msecs" "Elapsed time: 2566.682218 msecs" "Elapsed time: 2575.225692 msecs" nil user=> (dotimes [_ 10] (time (reduce + (range 100000000)))) "Elapsed time: 1935.942767 msecs" "Elapsed time: 1964.796084 msecs" "Elapsed time: 2321.384841 msecs" "Elapsed time: 1946.584133 msecs" "Elapsed time: 1987.454202 msecs" "Elapsed time: 1954.452801 msecs" "Elapsed time: 1945.051815 msecs" "Elapsed time: 1962.773062 msecs" "Elapsed time: 1974.039368 msecs" "Elapsed time: 1966.895303 msecs" nil Phew! Experiment confirms theory ;-) Kyle: On 'apply vs 'reduce, reduce is the rule (and reduce conveys more meaning both to the (human) reader and to the compiler/environment -- ie it can be optimized (see Rich's work on chunked seqs) and is already optimized for java arrays, lists, ranges and vectors). One notable exception is 'str where 'apply is preferred because Java strings concatenation isn't efficient. > I would have expected that 'apply would eventually blow up, because in > Common Lisp there is a maximum number of arguments. In Clojure apply is lazy but don't trust it to be strictly lazy: user=> (apply (fn [& _]) (map #(doto % println) (range 10))) 0 1 nil > My second > intuition was that if '+/2 is inlined then the reduce should be > faster, taking advantage of the inlined code. inlining is really interesting with primitive args. (dotimes [_ 10] (time (reduce #(+ %1 %2) (range 100000000)))) "Elapsed time: 2388.735091 msecs" "Elapsed time: 2462.450186 msecs" "Elapsed time: 2427.287407 msecs" "Elapsed time: 2435.715922 msecs" "Elapsed time: 2383.38923 msecs" "Elapsed time: 2404.126851 msecs" "Elapsed time: 2390.338506 msecs" "Elapsed time: 2399.320026 msecs" "Elapsed time: 2375.095648 msecs" "Elapsed time: 2459.116243 msecs" nil it's worst than + because it adds an indirection (the anonymous closure). user=> (dotimes [_ 10] (time (reduce #(+ (long %1) (long %2)) (range 100000000)))) "Elapsed time: 2679.988029 msecs" "Elapsed time: 1876.67846 msecs" "Elapsed time: 1862.06131 msecs" "Elapsed time: 1903.309569 msecs" "Elapsed time: 1943.72106 msecs" "Elapsed time: 1912.806865 msecs" "Elapsed time: 1860.759747 msecs" "Elapsed time: 1871.212752 msecs" "Elapsed time: 1894.235929 msecs" "Elapsed time: 1863.166198 msecs" nil seems marginally faster than plain + but it's not a good example because of the call to 'reduce that require the args and the return value to boxed. Not worth bothering. hth, Christophe -- Professional: http://cgrand.net/ (fr) On Clojure: http://clj-me.blogspot.com/ (en) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---