On Mon, May 9, 2011 at 6:55 PM, Andy Fingerhut <[email protected]> wrote:
> (ns movavg
> (:gen-class))
>
> (set! *warn-on-reflection* true)
>
>
> (defn sliding-window-moving-average [window lst]
> (let [w (int window)
> w-1 (int (dec w))]
> (loop [rolling-sum (apply + (take w lst))
> last-w (object-array (take w lst))
This is mutated in place and recurred with itself, so you can just
move it to the outer let (to some spot after w's initialization, of
course). That might afford a minor speedup, but not if the JIT is
optimizing away the repeated assignment of the reference to itself. It
will make the code a bit more concise and clear.
> i (int 0)
> avgs (transient [(/ rolling-sum w)])
> lst (drop w lst)]
You might get a significant speedup with
lst (seq (drop w lst))]
here and
> (if-let [lst (seq lst)]
(if lst
here and
> (let [old-val (aget last-w i)
> new-val (first lst)
> next-rolling-sum (- (+ rolling-sum new-val) old-val)]
> (aset last-w i new-val)
> (recur next-rolling-sum
> last-w
> (if (== i w-1) (int 0) (inc i))
> (conj! avgs (/ next-rolling-sum w))
> (rest lst)))
(next lst)))
here. Calling seq is fairly expensive.
--
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