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?
The python version I tried:
data = [None]*4000001
def sk(k):
if data[k] is None:
if k <= 55:
ans = ((100003 - 200003 * k + 300007 *
k**3) % 1000000) - 500000
else:
ans = ((data[k-24] + data[k-55] + 1000000)
% 1000000) - 500000
data[k] = ans
return ans
else:
return data[k]
a = map(sk, range(100000)) ; almost instant
--
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