title: Why is the Clojure Hello World program so slow compared to Java and Python?
body: <p><strong>Update</strong></p>
  
  <p>As suggested by many people, it looks like this was because of the fact that clojure code was first compiled and then executed. AOT compilation should help offset that. Given I found the practical Clojure AOT compilation process a little tricky to solve (classpath issues, directory issues et al), I've written up a small step by step process <a href="http://www.viksit.com/content/clojure-aot-compilation-tutorial" rel="noreferrer">here</a>, in case anyone is interested.</p>
  
  <hr>
  
  <p>Hi all,</p>
  
  <p>I'm reading "Programming Clojure" and I was comparing some languages I use for some simple code. I noticed that the clojure implementations were the slowest in each case. For instance,</p>
  
  <p><strong>Python - hello.py</strong></p>
  
  <pre><code>def hello_world(name):
    print "Hello, %s" % name
  
  hello_world("world")
  </code></pre>
  
  <p>and result,</p>
  
  <pre><code>$ time python hello.py
  Hello, world
  
  real    0m0.027s
  user    0m0.013s
  sys 0m0.014s
  </code></pre>
  
  <p><strong>Java - hello.java</strong></p>
  
  <pre><code>import java.io.*;
  
  public class hello {
    public static void hello_world(String name) {
      System.out.println("Hello, " + name);
    }
  
    public static void main(String[] args) {
      hello_world("world");
    }
  }
  </code></pre>
  
  <p>and result,</p>
  
  <pre><code>$ time java hello
  Hello, world
  
  real    0m0.324s
  user    0m0.296s
  sys 0m0.065s
  </code></pre>
  
  <p>and finally, </p>
  
  <p><strong>Clojure - hellofun.clj</strong></p>
  
  <pre><code>(defn hello-world [username]
    (println (format "Hello, %s" username)))
  
  (hello-world "world")
  </code></pre>
  
  <p>and results,</p>
  
  <pre><code>$ time clj hellofun.clj 
  Hello, world
  
  real    0m1.418s
  user    0m1.649s
  sys 0m0.154s
  </code></pre>
  
  <p>Thats a whole, garangutan 1.4 seconds! </p>
  
  <p>Does anyone have pointers on what the cause of this could be? Is Clojure really that slow, or are there JVM tricks et al that need to be used in order to speed up execution?</p>
  
  <p>More importantly - isn't this huge difference in performance going to be an issue at some point? (I mean, lets say I was using Clojure for a production system - the gain I get in using lisp seems completely offset by the performance issues I can see here). </p>
  
  <hr>
  
  <p>The machine used here is a 2007 Macbook Pro running Snow Leopard, a 2.16Ghz Intel C2D and 2G DDR2 SDRAM.</p>
  
  <p>BTW, the clj script I'm using is from <a href="http://mark.reid.name/sap/setting-up-clojure.html" rel="noreferrer">here</a> and looks like,</p>
  
  <pre><code>#!/bin/bash
  JAVA=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java
  CLJ_DIR=/opt/jars
  CLOJURE=$CLJ_DIR/clojure.jar
  CONTRIB=$CLJ_DIR/clojure-contrib.jar
  JLINE=$CLJ_DIR/jline-0.9.94.jar
  CP=$PWD:$CLOJURE:$JLINE:$CONTRIB
  
  # Add extra jars as specified by `.clojure` file
  if [ -f .clojure ]
  then
    CP=$CP:`cat .clojure`
  fi
  
  if [ -z "$1" ]; then
    $JAVA -server -cp $CP \
        jline.ConsoleRunner clojure.lang.Repl
  else
    scriptname=$1
    $JAVA -server -cp $CP clojure.main $scriptname -- $*
  fi
  </code></pre>