Hugh Winkler holding forth on computing and the Web

Thursday, January 01, 2009

Clojure uptick

Needing to build a standalone piece of server software as an add on to our Java product, I seized the opportunity to see how far Clojure has come. Getting my dev system set up, and learning the language, I noticed the results Google was returning for my queries were mostly very recent... and lots from just last month.



Could Clojure be approaching critical mass?

I've only written a hundred lines or so of code, but here are some random things I love:

1. Shorthand for maps: {:a 1 :b 3}.
2. sorted-map and hash-map -- thanks for letting me choose. Same shorthand syntax for either.
3. Shorthand to select a value from a map: the map is like a function name (my-map :a).
4. Shorthand for lambdas: #(...code...)
5. Shorthand for generating temporary symbols in macros: my-var#. No need to call (gensym).
4. No looping required -- use recur.
5. Java libraries are easy to use.

I haven't yet fiddled with modifying state.

It's free, it's functional, it's got a hardened standard library, it's got a REPL. You can write a program and deliver it on any platform. What could hold it back?

5 comments:

Anonymous said...

It'd be nice if it were more efficient. I've seen no evidence that it can perform anywhere near Java code, even with type annotations and unsafe math.

I'd be happy to take a 5x hit for what Clojure offers, but from some limited personal testing it looks a lot worse than that. Compare to SBCL...

I hope it's just a case of getting things running right first, because Scala isn't my thing.

Anonymous said...

Usually the performance hit is in a very small part of the application, so you can always re-code those parts in java. Or C (through JNI) if it's really the case. It reminds me on how we used to have "asm" in C... a bit nostalgic.

Anyways, in the words of the prophets, "Premature optimization is the root of all evil"

Anonymous said...

I agree with the sentiment, but part of the reason for having a compiler is to do much of that optimization for us automatically. If C compilers were up to the task back then, fewer programmers would have had to resort to assembly.

If SBCL can get within a stone-throw of C, then by golly, maybe Clojure can get within Java too -- minus the overhead of persistent data structures, of course.

The stuff I work on is CPU-bound, so this matters to me.

hughw said...

@anon #1 -- I hope as well "it's just a case of getting things running right first".

Do you have an example?

(I'm encouraged by Rich's assertion that he can get the same speed as Java for the code snippet there. But yeah, reduce over an array of primitives isn't everything).

An example from what you're working on would be great.

Anonymous said...

Based on what I've seen, I'm just a wee bit dubious of Rich's claim. What he says is probably true, but I'm unable to replicate the feat with trivial code.

I took the code here: http://shootout.alioth.debian.org/gp4/benchmark.php?test=recursive&lang=javaxx

And translated it into this attempt at a riced-out version: http://paste.lisp.org/display/72996

It's still several times slower than the almost-equivalent Java code. Give it a try on your machine, and if that's still the case for you, let me know if you can figure out how to improve upon it.

Also, the optimizer, if Clojure has one, is kinda lame; there's a large performance difference between (- n 1) and (dec n), the same for inc, (= n 0) is a lot slower than (zero? n)... a peephole optimizer for this is trivial, especially in a Lisp. :(