Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Rough answers:</p> <ol> <li>Scala's static typing is helping it quite a bit here - this means that it uses the JVM pretty efficiently without too much extra effort.</li> <li>I'm not exactly sure on the Ruby/Python difference, but I suspect that <code>(2...n).all?</code> in the function <code>is-prime?</code> is likely to be quite well optimised in Ruby (EDIT: sounds like this is indeed the case, see Julian's answer for more detail...)</li> <li>Ruby 1.9.3 is just much better optimised</li> <li>Clojure code can certainly be accelerated a lot! While Clojure is dynamic by default, you can use type hints, primitive maths etc. to get close to Scala / pure Java speed in many cases when you need to.</li> </ol> <p>Most important optimisation in the Clojure code would be to use typed primitive maths within <code>is-prime?</code>, something like:</p> <pre><code>(set! *unchecked-math* true) ;; at top of file to avoid using BigIntegers (defn ^:static is-prime? [^long n] (loop [i (long 2)] (if (zero? (mod n i)) false (if (&gt;= (inc i) n) true (recur (inc i)))))) </code></pre> <p>With this improvement, I get Clojure completing 10k in 0.635 secs (i.e. the second fastest on your list, beating Scala)</p> <p><strong>P.S.</strong> note that you have printing code inside your benchmark in some cases - not a good idea as it will distort the results, especially if using a function like <code>print</code> for the first time causes initialisation of IO subsystems or something like that!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload