Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can any Clojure implementation start fast?

$ 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.

like image 729
Vi. Avatar asked Apr 09 '13 23:04

Vi.


Video Answer


2 Answers

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
like image 177
bmaddy Avatar answered Sep 30 '22 03:09

bmaddy


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.

like image 45
mikera Avatar answered Sep 30 '22 03:09

mikera