Hi Nicholas,

Thanks for the advice. I already tried (set! *warn-on-reflection*
true), but it doesn't generate any warnings for any of these
functions.

I tried adding ^doubles in gaussian-matrix1 as you suggested but that
didn't really change the speed at all. And changing next-gaussian to a
macro didn't make much difference to gaussian-matrix1, and it doesn't
work with gaussian-matrix2 at all and I'm not quite sure yet how to
fix it so it does.

I would have thought gaussian-matrix1 would be faster than gaussian-
matrix2, but it turns out it's the opposite. Then I thought that the
reason using aset was slower must have something to do with
reflection, but that doesn't seem to be the case.

So I'm a bit confused now.

More generally though, the main reason I wanted to use Java arrays was
because I have to use FFT's in my simulation and I figured it would be
better to do everything in Java arrays rather than copy a Clojure
vector into a 2d array and back every time I needed to do an FFT. But
am I wrong in thinking that?

Thanks,

Ranjit



On Sep 19, 11:20 am, Nicolas Oury <nicolas.o...@gmail.com> wrote:
> A first good start is to put
> (set! *warn-on-relection* true) at the start of the file and removes
> all reflective access.
>
> Before the 1.3 release, function cannot receive/returns primitive so
> you might consider
> (defmacro next-gaussian []
>   `(.nextGaussian  ^Random r))
>
> (^Random is here to make sure r is seen with the right type)
>
> in both function, add ^doubles before arr at the binding point.
> (defn ... [^doubles arr....])
>
> then set will not be rflective and be as fast as a set in java.
>
> Ask other questions of you need more help.
> The best reference on all that: clojure.org/java_interop
>
>
>
> On Sun, Sep 19, 2010 at 3:29 PM, Ranjit <rjcha...@gmail.com> wrote:
> > Hi,
>
> > I'm trying learn Clojure to see if I can use it in my simulations, and
> > one thing I need to do is generate arrays of normally distributed
> > numbers.
>
> > I've been able to come up with the following two ways of doing this.
> > gaussian-matrix2 is a lot faster than gaussian-matrix1, but I'm not
> > sure why. And it's still slower than it should be I think. Is there
> > anything I can do to speed this up still further?
>
> > Thanks,
>
> > -Ranjit
>
> > (import java.util.Random)
> > (def r (Random. ))
>
> > (defn next-gaussian [] (.nextGaussian r))
>
> > (defn gaussian-matrix1 [arr L]
> >     (doseq [x (range L) y (range L)] (aset arr x y (next-gaussian))))
>
> > (defn gaussian-matrix2 [L]
> >     (into-array (map double-array (partition L (repeatedly (* L L)
> > next-gaussian)))))
>
> > --
> > 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
>
> --
> Sent from an IBM Model M, 15 August 1989.

-- 
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

Reply via email to