Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:31:25+00:00 2026-05-31T19:31:25+00:00

I have a question about why there is such a difference in speed between

  • 0

I have a question about why there is such a difference in speed between the loop method and the iterate method in clojure. I followed the tutorial in http://www.learningclojure.com/2010/02/clojure-dojo-2-what-about-numbers-that.html and defined two square-root methods using the Heron method:

(defn avg [& nums] (/ (apply + nums) (count nums)))
(defn abs [x] (if (< x 0) (- x) x))
(defn close [a b] (-> a (- b) abs (< 1e-10) ) )

(defn sqrt [num]
  (loop [guess 1]
    (if (close num (* guess guess))
        guess
     (recur (avg guess (/ num guess)))
)))

(time (dotimes [n 10000] (sqrt 10))) ;;"Elapsed time: 1169.502 msecs"


;; Calculation using the iterate method
(defn sqrt2 [number]
    (first (filter #(close number (* % %)) 
        (iterate #(avg % (/ number %)) 1.0))))

(time (dotimes [n 10000] (sqrt2 10))) ;;"Elapsed time: 184.119 msecs"

There is about a x10 increase in speed between the two methods and I’m wondering what is happening below the surface to cause the two to be so pronouced?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:31:27+00:00Added an answer on May 31, 2026 at 7:31 pm

    Your results are surprising: normally loop/recur is the fastest construct in Clojure for looping.

    I suspect that the JVM JIT has worked out a clever optimisation for the iterate method, but not for the loop/recur version. It’s surprising how often this happens when you use clean functional code in Clojure: it seems to be very amenable to optimisation.

    Note that you can get a substantial speedup in both versions by explicitly using doubles:

    (set! *unchecked-math* true)
    
    (defn sqrt [num]
      (loop [guess (double 1)]
        (if (close num (* guess guess))
          guess
          (recur (double (avg guess (/ num guess)))))))
    
    (time (dotimes [n 10000] (sqrt 10)))
    => "Elapsed time: 25.347291 msecs"
    
    
    (defn sqrt2 [number]
      (let [number (double number)]
        (first (filter #(close number (* % %)) 
          (iterate #(avg % (/ number %)) 1.0)))))
    
    (time (dotimes [n 10000] (sqrt 10)))
    => "Elapsed time: 32.939526 msecs"
    

    As expected, the loop/recur version now has a slight edge. Results are for Clojure 1.3

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a question about the branchtables. There are two ways to declare such
I have a question about this question . I posted a reply there but
I have a simple question about Java's XML API and I hope there's a
There is a question I have been wondering about for ages and I was
I have a fairly simple question about these 2 templating engines. I'm trying to
There have been some questions about whether or not JavaScript is an object-oriented language.
There have been several questions already posted with specific questions about dependency injection ,
OK, I know there have already been questions about getting started with TDD ..
There have been several questions recently about database indexing and clustered indexing and it
I know there have been questions in the past about SQL 2005 versus Lucene.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.