For most array operations (e.g. dot products on vectors), I strongly
recommend trying out the recent core.matrix implementations. We've put a
lot of effort into fast implementations and a nice clean Clojure API so I'd
love to see them used where it makes sense!
For example vectorz-clj can be over 100x faster than a naive map / reduce
implementation:
(let [a (vec (range 10000))
b (vec (range 10000))]
(time (dotimes [i 100] (reduce + (map * a b)))))
"Elapsed time: 364.590211 msecs"
(let [a (array :vectorz (range 10000))
b (array :vectorz (range 10000))]
(time (dotimes [i 100] (dot a b))))
"Elapsed time: 3.358484 msecs"
On Monday, 22 December 2014 17:31:41 UTC+8, Henrik Eneroth wrote:
>
> Interesting read Jose, thanks!
>
> It might be interesting to try a transducer on
>
> (defn dot-prod
> "Returns the dot product of two vectors"
> [v1 v2]
> (reduce + (map * v1 v2)))
>
> if you can get your hands on the 1.7 alpha and the time and inclination to
> do it. Transducers have shown to be faster than running functions in
> sequence. Although I don't know how likely they are to beat native arrays,
> probably not very much.
>
>
> On Sunday, December 21, 2014 7:10:41 PM UTC+1, Jose M. Perez Sanchez wrote:
>>
>>
>> Regarding the speed optimizations, execution time for a given model was
>> reduced from 2735 seconds to 70 seconds, over several versions by doing
>> several optimizations.
>>
>> The same calculation implemented in C# takes 12 seconds using the same
>> computer and OS. Maybe the Clojure code can still be improved, but for the
>> time being I'm happy with the Clojure version being six times slower, since
>> the new software has many advantages.
>>
>> For these tests the model was the circle with radius 1 using the
>> "diffmr1" tracker, the simulation was run using 10000 particles and 10000
>> total random walk steps.
>>
>> These modifications in the critical parts of the code accounted for most
>> of the improvement:
>>
>> - Avoid reflection by using type hints.
>> - Use Java arrays.
>> - In some cases call Java arithmetic functions directly instead of
>> Clojure ones.
>> - Avoid using partial functions in the critical parts of the code.
>>
>> Avoiding lazyness did not help much. Regarding the use of Java arrays,
>> there are many small functions performing typical vector operations on
>> arrays, such as the following example:
>>
>> Using Clojure types:
>>
>> (defn dot-prod
>> "Returns the dot product of two vectors"
>> [v1 v2]
>> (reduce + (map * v1 v2)))
>>
>> Using Java arrays:
>>
>> (defn dot-prod-j
>> "Returns the dot product of two arrays of doubles"
>> [^doubles v1 ^doubles v2]
>> (areduce v1 i ret 0.0
>> (+ ret (* (aget v1 i)
>> (aget v2 i)))))
>>
>>
>> This gives a general idea of which optimizations helped the most. These
>> changes are not in the public repository, since previous commits have been
>> omitted because the code code was not ready for publication (different
>> license disclaimer, contained email addresses, etc.). If anyone is
>> interested in the diffs and the execution times over several optimizations,
>> please contact me.
>>
>> Kind regards,
>>
>> Jose.
>>
>>
>> On Sunday, December 21, 2014 3:38:35 AM UTC-5, Jose M. Perez Sanchez
>> wrote:
>>>
>>>
>>> Hi everyone:
>>>
>>> Sorry that it has taken so long. I've just released the software in
>>> GitHub under the EPL. It can be found at:
>>>
>>> https://github.com/iosephus/gema
>>>
>>>
>>> Kind regards,
>>>
>>> Jose.
>>>
>>>
--
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.