I often see people trying to call Clojure code from Java and having some trouble doing this. So I decided to write a quick utility to do this (inspired by Rich Hickey’s Lightening Talk), and provide a simple example.
Posts Tagged ‘java’
Tags: clojure, interop, java
Clojure is certainly my language of choice nowadays.
But recently I spent a day working on an old ~35k LOC Java code base, and it reminded me what I still love about Java.
Tags: java, macros, optimization, performance
Clojure performs exceptionally well considering it is a dynamic language.
My rule of thumb is:
- It’s 5-10x slower than optimised Java if you write idiomatic Clojure (with liberal use of higher order functions, lazy sequences and all that goodness)
- It’s about the same speed as Java if you really optimise hard – you can basically generate approximately the same JVM bytecode.
This StackOverflow question prompted me to write a quicksort implementation in Clojure, which I’ve included here to demonstrate that matching Java speed is entirely possible within Clojure.
Warning against premature optimisation: this is not pretty code. You shouldn’t be writing code like this unless you have profiled your code and identified a real performance issue.
Clojure is currently my language of choice for new projects. The reasons probably won’t come as a surprise – among other things I love the expressive style, the level of productivity which enables you to “get things done”, the interactive development at the REPL, the meta-programming capabilities, the approach to concurrency and the various advantages of functional programming.
Yet in several recent projects, I’ve found myself using a polyglot mix of Clojure and Java.
Why would I subject myself to this?