On Jan 14, 11:27 am, Asbjørn  Bjørnstad <asbj...@gmail.com> wrote:
> On Jan 14, 12:20 pm, "Mark H." <mark.hoem...@gmail.com> wrote:
>
>
>
> > I humbly propose that folks shouldn't complain about Clojure being
> > slow for their apps until they have at least one of the following:
>
> > 1. A targeted benchmark for an important bottleneck in their
> > application, implemented in both Clojure and the current
> > implementation language, with performance results; or
>
> > 2. A performance model based on an understanding of the Clojure and
> > HotSpot compilation processes, that highlights which particular
> > features of the latter would make their application slower than in its
> > current implementation language.
>
> > This is not out of elitism but out of an earnest desire to be helpful;
> > it's hard to give good advice on language choice and code optimization
> > when we don't know what the bottlenecks are.  Also it's really hard
> > for anyone to optimize their own code unless they themselves know
> > where the bottlenecks are.  The latter means that #1 above is
> > surprisingly easy; it often can be done with a simple function trace
> > of a typical run.
>
> I'm not complaining, but I find the topic interesting as I just tried
> translating a fluid dynamics algorithm to clojure.
> (http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/
> GDC03.pdf)
> I got no experience with this kind of stuff, it's just something I
> tried out for fun. So the performance I get may be as expected. Or
> there may be obvious ways of speeding it up that I don't see.
>
> Anyway, here is a core part of the algorithm. It's a diffusion
> step, this gets called 3 times and in total this takes up more than
> one second of cpu time on my machine which makes framerates very
> slow. If anyone got any improvements I'd be happy to hear about it:
>
> (def grid-size 300)
>
> (defn make-grid []
>   (make-array Float/TYPE (* (+ grid-size 2)
>                             (+ grid-size 2))))
>
> (defn diffuse [grid diff-ratio dt]
>   (let [a (float (* dt diff-ratio grid-size grid-size))
>         a4-1 (float (+ 1 (* 4 a)))
>         grid #^floats (deref grid)
>         diffused-grid #^floats (make-grid)
>         line-length (int (+ grid-size 2))]
>     (dotimes [n 20]
>       (dotimes [y grid-size]
>         (let [line-offset (* (inc y) line-length)]
>          (loop [x (int 1)
>                 c (+ x line-offset)]
>            (aset diffused-grid c
>                        (float (/ (+ (aget grid c)
>                                     (*  a
>                                         (+ (+ (aget diffused-grid (dec
> c))
>                                               (aget diffused-grid (inc
> c)))
>                                            (+ (aget diffused-grid (- c
> line-length))
>                                               (aget diffused-grid (+ c
> line-length))))))
>                                  a4-1)))
>            (when (< x grid-size)
>              (recur (inc x)
>                     (inc c)))))))
>     diffused-grid))
>
> (def foo (ref (make-grid)))
>
> (time (diffuse foo 0.00025 0.002))
>

Try this (and make sure you are using -server):

(defn diffuse [grid diff-ratio dt]
  (let [a (float (* dt diff-ratio grid-size grid-size))
        a4-1 (float (+ 1 (* 4 a)))
        grid #^floats (deref grid)
        diffused-grid #^floats (make-grid)
        grid-size (int grid-size)
        line-length (int (+ grid-size 2))]
    (dotimes [n 20]
      (dotimes [y grid-size]
        (let [line-offset (* (inc y) line-length)]
         (loop [x (int 1)
                c (int (+ x line-offset))]
           (aset diffused-grid c
                       (/ (+ (aget grid c)
                                    (*  a
                                        (+ (+ (aget diffused-grid
(unchecked-dec c))
                                              (aget diffused-grid
(unchecked-inc c)))
                                           (+ (aget diffused-grid
(unchecked-subtract c line-length))
                                              (aget diffused-grid
(unchecked-add c line-length))))))
                                 a4-1))
           (when (< x grid-size)
             (recur (unchecked-inc x)
                    (unchecked-inc c)))))))
    diffused-grid))

(time (diffuse foo 0.00025 0.002))
"Elapsed time: 25.588 msecs"

Rich

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