FWIW, out of the box on Clojure 1.3.0 with no changes at all, your code took about 8 seconds on my machine on the first run. Since your sk function will reuse the arrays, subsequent runs don't calculate any new values, they just return the pre-calculated ones. Subsequent runs took about 2 seconds on my machine.
I took David's version and ran that on Clojure 1.3.0 with about the same results as he saw. Even naming the anonymous function and using recursive calls (instead of the aget vs substitution that David did) produced reasonable results (closer to 400ms instead of closer to 300ms) - just to address Ken's comment since the ss / vs arrays essentially memoize the calls, even in your original code. Out of curiosity, I modified David's version to pull the array creation out: (def skk (sk (long-array 4000001) (boolean-array 4000001))) (time (dorun (map skk (range 100000)))) First run, same (as expected). Subsequent runs (only retrieving numbers, not calculating them), about 10ms. Eliminating reflection and using unchecked math are going to get you some pretty darn fast Clojure code! Sean On Thu, Jul 21, 2011 at 6:32 AM, Oskar <oskar.kv...@gmail.com> wrote: > I was doing some project euler problems and I wrote the following > code. It is really slow, and I would like to know why and how it can > be made faster. Not that I care much about this code in particular, > but I want to understand Clojure better. > > (def vs (int-array 4000001)) > (def ss (boolean-array 4000001)) > > (defn sk [k] > (if (aget ss k) > (aget vs k) > (let [ans > (if (< k 56) > (- (mod (+ 100003 (- (* k 200003)) (* 300007 k k k)) > 1000000) 500000) > (- (mod (+ (sk (- k 24)) (sk (- k 55))) 1000000) > 500000))] > (do (aset vs k ans) (aset ss k true) ans)))) > > (time (dorun (map sk (range 100000)))) > > The call above takes 20 seconds, which is surprisingly slow (at least > to me). The "same thing" in Python it takes under under 1 (not sure > exactly how long). Why so slow? -- 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