On Tue, Nov 1, 2011 at 4:29 AM, redraiment <[email protected]> wrote:
> user=> (def n 100000000)
> #'user/n
> user=> (time (loop [cnt 100000000 sum 0] (if (zero? cnt) sum (recur
> (dec cnt) (+ sum cnt)))))
> "Elapsed time: 605.564858 msecs"
> 5000000050000000
> user=> (time (loop [cnt n sum 0] (if (zero? cnt) sum (recur (dec cnt)
> (+ sum cnt)))))
> "Elapsed time: 3707.245668 msecs"
> 5000000050000000
>
> Why did the second loop so slow?!

David beat me to it! n being a variable means the loop is using a Long
for cnt instead of long - you can do this to get the same performance:

(time (loop [cnt (long n) sum 0] (if (zero? cnt) sum (recur (dec cnt)
(+ sum cnt)))))
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

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

Reply via email to