Using unchecked-remainder and adding a type declaration to the last
constant keeps the primitives unboxed and improves performance by a
factor of 6 for me. I couldn't say whether there's a reason rem and
mod don't work like you'd expect here.
(defn NF-mod-limit [p q limit]
(let [p (long p) q (long q) limit (long limit)]
(loop [n (long 0), nf (long 0), z (long 0), S (long 290797)]
(if (= n (inc q))
nf
(recur
(inc n)
(unchecked-remainder
(+ nf (* (unchecked-remainder S p) z))
limit)
(unchecked-remainder (inc (* p z)) limit)
(unchecked-remainder (* S S) (long 50515093)))))))
On May 3, 1:21 pm, Brian Watkins <[email protected]> wrote:
> Any ideas about this?
>
> On May 2, 1:44 am, Brian Watkins <[email protected]> wrote:
>
>
>
>
>
> > I'm trying to speed up computing terms of a simple recurrence where
> > the terms are computed modulo some value each iteration,
>
> > (defn NF-mod-limit [p q limit]
> > (loop [n 0, nf 0, z 0, S 290797]
> > (if (= n (inc q)) nf
> > (recur (inc n)
> > (mod (+ nf (* (mod S p) z)) limit)
> > (mod (inc (* p z)) limit)
> > (mod (* S S) 50515093))))))
>
> > So I added in some type hinting,
>
> > (defn NF-mod-limit [p q limit]
> > (let [p (long p) q (long q) limit (long limit)]
> > (loop [n (long 0), nf (long 0), z (long 0), S (long 290797)]
> > (if (= n (inc q)) nf
> > (recur (inc n)
> > (long (mod (+ nf (* (long (mod S p)) z)) limit))
> > (long (mod (inc (* p z)) limit))
> > (long (mod (* S S) 50515093)))))))
>
> > But it doesn't run any faster. Also it doesn't work without casting
> > to long within the recur. Is that maybe a problem with mod and
> > primitive arithmetic or does this simply not speed up any more in
> > Clojure?
>
> > --
> > 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
> > athttp://groups.google.com/group/clojure?hl=en
>
> --
> 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
> athttp://groups.google.com/group/clojure?hl=en
--
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