On Tue, Aug 18, 2009 at 3:32 PM, Aaron Cohen<remled...@gmail.com> wrote:
> On Tue, Aug 18, 2009 at 11:28 AM, Brad
> Beveridge<brad.beveri...@gmail.com> wrote:
>>
>> On 2009-08-17, at 8:58 PM, FFT <fft1...@gmail.com> wrote:
>>
>>> On Mon, Aug 17, 2009 at 9:25 AM, Bradbev<brad.beveri...@gmail.com>
>>> wrote:
>>>>
>>>> Ah, that makes more sense re the "cheating" then.  Your insight for
>>>> array range check elimination got me thinking - why can't the
>>>> accessor
>>>> macros (posx, etc) that use aset/aget have their ranges eliminated by
>>>> the JVM?  After all, it should be a simple constant fold.  I found
>>>> another 2-3x speed up by coercing the indexes with (int x), ie
>>>> (defmacro mass [p] `(double (aget ~p (int 0))))
>>>
>>> I'm not seeing this. Maybe you are running this on "-client"?
>> I'm running Java 1.5 32bit on OS X 10.5 with -server.
>>>
>>>> I don't have the Java version running on my machine, but I saw
>>>> runtimes go from 833ms to 295ms for 100000 iterations, a 2.8x speed
>>>> up, which should put the "no cheating" version on the same standing
>>>> as
>>>> the Java implementation.
>>>
>>> You can't get a consistent timing for anything less than 1-10M
>>> iterations here.
>> Why do you think that?  Everything I've read says that hotspot kicks
>> in at 10,000, and I always do a warmup run.
>> I see consistent enough timings, within 50ms each run. When coerced
>> ints gives an immediate 3x speedup something is happening. What JVM
>> are you running & what settings?  I'll compile the java version soon
>> so I can do a direct compare on a single machine. I take it that your
>> setup is showing clojure 3x slower than the java version?
>>
>> Brad
>>
>
> I don't see much of any difference here from those coercions either.
> What clojure version are you using?
> -- Aaron
>

I reworked advance! a little bit and while it didn't have much
performance impact, I find this version a little clearer:

(defmacro doarray
  "Executes an expression for each element of array a (presumably for
its side-effects), using an index named idx,
   beginning with element 'start'"
  [a idx start expr]
  `(let [a# ~a end# (int (alength a#))]
     (loop  [~idx (int ~start)]
       (if (< ~idx end#)
                 (do
                 ~expr
         (recur (unchecked-inc ~idx)))))))

(defn advance! [#^"[Ljava.lang.Object;" bodies delta-t]
  (let [delta-t (double delta-t)]
    (doarray bodies i1 0
                (doarray bodies i2 (unchecked-inc i1)
            (let [#^doubles b1 (aget bodies i1)
                    #^doubles b2 (aget bodies i2)
                    delta-posx (- (posx b1) (posx b2))
                    delta-posy (- (posy b1) (posy b2))
                    delta-posz (- (posz b1) (posz b2))
                    dist-squared (+ (+ (* delta-posx delta-posx)
                                       (* delta-posy delta-posy))
                                    (* delta-posz delta-posz))
                    dist (Math/sqrt dist-squared)
                    mag (/ delta-t (* dist-squared dist))
                    b1-scale (* (- mag) (mass b2))
                    dv1x (* delta-posx b1-scale)
                    dv1y (* delta-posy b1-scale)
                    dv1z (* delta-posz b1-scale)
                    b2-scale (* mag (mass b1))
                    dv2x (* delta-posx b2-scale)
                    dv2y (* delta-posy b2-scale)
                    dv2z (* delta-posz b2-scale)]
                (add-to-vel! b1 dv1x dv1y dv1z)
                (add-to-vel! b2 dv2x dv2y dv2z))))
        (doarray bodies i 0
        (let [#^doubles b (aget bodies i)]
          (set-posx! b (+ (posx b) (* (velx b) delta-t)))
          (set-posy! b (+ (posy b) (* (vely b) delta-t)))
          (set-posz! b (+ (posz b) (* (velz b) delta-t)))))))

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