Slowdowns wrt math are caused mainly by boxing and range check, see this 
thread [1]

[1] https://groups.google.com/d/msg/clojure/kcx5eKdMxcs/Wy4_IHrSEaMJ

Jozef

On Wednesday, October 15, 2014 9:53:40 PM UTC+2, Jony Hudson wrote:
>
> Hi all,
>
>  another performance question ... this time about arithmetic on vectors 
> :-) Let's say I have two vectors of numbers (specifically, things with type 
> clojure.lang.PersistentVector, containing things of type java.lang.Double). 
> And let's say I want to sum the differences [1] between corresponding 
> elements of the lists i.e. (a1 - b1) + (a2 - b2) + ...
>
> Any suggestions on how to do this quickly. What I find is that if I use 
> the 'obvious' high-level construction:
>
> (reduce + (mapv #(- %1 %2) a b))
>
>
> then it goes pretty slowly. On my laptop I measure about 180us for 1000 
> element lists.
>
> If I try using `loop`:
>
> (loop [sum 0.0 i 0]  
>  (if (< i 1000)
>    (recur (+ sum (- (nth a i) (nth b i))) (inc i))
>    sum))
>
> it does better, at about 100us.
>
> But these are still a ways off what I might think is the best that could 
> be done. If I run a similar computation in Java, it takes about 8us, which 
> ties up with what my gut feeling would be.
>
> So ... does anyone have any advice on closing the gap here?
>
> Thanks in advance, again,
>
>
> Jony
>
>
> [1] Actually I want to sum the absolute differences, but that brings in 
> java interop which I want to leave out lest it confuse matters.
>

-- 
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/d/optout.

Reply via email to