On Mon, May 3, 2010 at 4:21 PM, Brian Watkins <wildu...@gmail.com> wrote:
> Any ideas about this? > > On May 2, 1:44 am, Brian Watkins <wildu...@gmail.com> 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? > mod is a bit more general than Java's % operator. If you just want the % operator, then it looks like you need to use rem not mod. That seems to speed things up ~2X for me. Do you have some sample values you're passing to your function and the corresponding runtimes that you are seeing? David -- 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