This is a case where I would definitely recommend using core.matrix /
vectorz-clj
(let [a (array :vectorz (range 1000))
b (array :vectorz (range 1000 2000))]
(criterium/quick-bench (esum (sub a b))))
=> Execution time mean : 2.266914 µs
Or it's even faster if you notice that you can switch the operation order:
(let [a (array :vectorz (range 1000))
b (array :vectorz (range 1000 2000))]
(c/quick-bench (- (esum a) (esum b))))
=> Execution time mean : 1.798722 µs
On Thursday, 16 October 2014 03:53:40 UTC+8, 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 [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
---
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 [email protected].
For more options, visit https://groups.google.com/d/optout.