On Jun 23, 2009, at 12:41 PM, Christophe Grand wrote:

> On Tue, Jun 23, 2009 at 8:02 PM, Daniel Lyons  
> <[email protected]> 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 ;-)

Wish it were so, because I like your theory better than my reality: :)

user>  (dotimes [_ 10] (time (apply + (range 100000000))))
"Elapsed time: 3306.787 msecs"
"Elapsed time: 2920.778 msecs"
"Elapsed time: 2976.063 msecs"
"Elapsed time: 2889.74 msecs"
"Elapsed time: 3005.505 msecs"
"Elapsed time: 3075.499 msecs"
"Elapsed time: 2898.838 msecs"
"Elapsed time: 2900.19 msecs"
"Elapsed time: 3003.037 msecs"
"Elapsed time: 2954.846 msecs"
nil

user>  (dotimes [_ 10] (time (reduce + (range 100000000))))
"Elapsed time: 3742.196 msecs"
"Elapsed time: 3711.182 msecs"
"Elapsed time: 3646.465 msecs"
"Elapsed time: 3652.413 msecs"
"Elapsed time: 3650.788 msecs"
"Elapsed time: 3648.032 msecs"
"Elapsed time: 3641.9 msecs"
"Elapsed time: 3735.167 msecs"
"Elapsed time: 3682.609 msecs"
"Elapsed time: 3647.555 msecs"

I agree that the apply version should be slower, but it doesn't seem  
to be the case on my computer. (I'm surprised at the variability  
though.) I'm running a Clojure version from a couple weeks ago on  
Apple's Java 1.6 if that lends a clue as to the discrepancy.

At this point I'd ask if anybody has a stylistic reason to prefer one  
over the other, since the benchmarks seem less reliable than usual.

—
Daniel Lyons


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to