On Thu, Jul 21, 2011 at 9:32 AM, Oskar <oskar.kv...@gmail.com> wrote: > Hi! > > 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?
I'd guess boxed arithmetic. But first you might want to look into memoizing sk and making the recursive calls use the memoized version. If that doesn't make it fast enough, try using Clojure 1.3 (if you aren't already) and using primitives in sk including for its arguments and return value. -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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