5
$ time java -jar clojure-1.4.0.jar -e '(println "Hello world")'
Hello world

real    0m4.586s

$ time python clojure.py  -c '(py/print "Hello world")'

real    0m14.690s

$ time mono Clojure.Main.exe -e '(println "hello world")'
hello world

real    0m4.843s

/* clojure-metal is not tested due to not being written at the moment */

Can Clojure startup time be little, as like when I run Perl or Python scripts? Is slow startup time an underlying framework's or Clojure's issue (which can be fixed sooner or later) or it is by design?

Note: I already know about start-persistent-server-than-connect-to-it workaround.

Vi.
  • 37,014
  • 18
  • 93
  • 148

2 Answers2

4

ClojureScript can startup pretty quick. The number is a little misleading though because it does dead code removal when compiling. That means this snippet gets compiled down to just print("hello world");. Here's what I get on my machine:

$ echo '(js/print "hello world")' > hello.cljs
$ cljsc hello.cljs '{:optimizations :advanced}' > hello.js
$ time rhino hello.js 
hello world

real    0m0.466s

For comparison, here's what I get using normal clojure:

$ time java -jar clojure-1.4.0.jar -e '(println "hello world")'
hello world

real    0m1.369s
bmaddy
  • 876
  • 9
  • 16
3

The startup time is mostly because of the work that Clojure itself does in terms of initialisation. Some of these tasks are quite significant, e.g. loading and compiling the core Clojure namespaces. Running on different platform implementations won't really change this.

However, there is a big potential for this to be optimised in the future:

  • Large parts of Clojure itself could be ahead-of-time compiled
  • Performance of the Clojure compiler itself could be enhanced
  • Lazy loading and/or compilation could reduce the perceived latency (possibly the biggest win: most code doesn't need all of the namespaces in clojure.core and other dependencies, or at least not immediately)
  • Someone very clever may figure out how to do parallel loading and compilation

Note that although the JVM is often (unjustly) blamed, the JVM is largely irrelevant here: modern JVMs have a startup time of about 0.1secs.

I actually exploit this for GUI apps written in Clojure: by writing the startup code in pure Java with a main method, you can have a splash screen and first screen of your GUI appear pretty much immediately, and then load Clojure along with the rest of your application code in the background.

mikera
  • 105,238
  • 25
  • 256
  • 415
  • One limitation of the JVM is that objects must be copied on initialization, "You can't embed any composite constants in byte code. Not even arrays." https://groups.google.com/forum/#!topic/clojure/PsgKVlWZjOw – noisesmith Aug 26 '13 at 20:24