title: Clojure performance really bad on simple loop versus Java
body: <p>Spoiler alert, this is problem 5 of Project Euler.</p>
  
  <p>I am attempting to learn Clojure and solved problem 5, but it is a couple orders of magnitude slower (1515 ms in Java versus 169932 ms in Clojure). I even tried using type hinting, unchecked math operations, and inlining functions all for naught.</p>
  
  <p>Why is my Clojure code so much slower?</p>
  
  <p>Clojure code:</p>
  
  <pre><code>(set! *unchecked-math* true)
  (defn divides? [^long number ^long divisor] (zero? (mod number divisor)))
  
  (defn has-all-divisors [divisors ^long num]
    (if (every? (fn [i] (divides? num i)) divisors) num false))
  
  (time (prn (some (fn [^long i] (has-all-divisors (range 2 20) i)) (iterate inc 1))))
  </code></pre>
  
  <p>Java code:</p>
  
  <pre><code>public class Problem5 {
    public static void main(String[] args) {
      long start = System.currentTimeMillis();
      int i = 1;
      while(!hasAllDivisors(i, 2, 20)) {
        i++;
      }
      long end = System.currentTimeMillis();
      System.out.println(i);
      System.out.println("Elapsed time " + (end - start));
    }
  
    public static boolean hasAllDivisors(int num, int startDivisor, int stopDivisor) {
      for(int divisor=startDivisor; divisor&lt;=stopDivisor; divisor++) {
        if(!divides(num, divisor)) return false;
      }
      return true;
    }
  
    public static boolean divides(int num, int divisor) {
      return num % divisor == 0;
    }
  }
  </code></pre>