After updating from Clojure 1.2 to Clojure 1.3 Alpha 1, I noticed that
one of my Project Euler solutions became dramatically slower.  The
solution was for Problem 14, finding the number less than N that
produces the longest Collatz sequence.

For N = 100,000, the time required to find the answer was as follows:

Clojure 1.2 = 1327.45 msecs
Clojure 1.3 Alpha 1 = 191186.76 msecs

(For Problem 14, N is actually 1,000,000, but I ran out of patience
waiting for the code to produce the answer in Clojure 1.3 Alpha 1)

Has anyone run into something like this, or know what might have
caused the dramatic difference in speed?

The code:

(defn next-term [n]
  (if (even? n) (/ n 2)
      (inc (* n 3))))

(defn count-terms [n]
  (if (= 1 n) 1
      (inc (count-terms (next-term n)))))

(let [pair (juxt identity count-terms)
       pairs (map pair (range 1 100000))]
   (println (first (apply max-key second pairs))))

Machine specs:

Intel Core 2 Duo T9300 @ 2.5 GHz
2 GB RAM
JVM is running on server mode

P.S. I originally wanted to write that last part of the code a little
more elegantly, like this:

(println (apply max-key count-terms (range 1 100000)))

But that made things even slower:

Clojure 1.2 = 2764.48 msecs
Clojure 1.3 Alpha 1 = 740025.36 msecs

I think I read somewhere that max-key applies f more times than is
necessary, so should not be pass any f that takes significant time to
compute.

-- 
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

Reply via email to