Thoughts and bits: From Elegance to Speed, with Clojure

submited by
Style Pass
2024-06-07 22:30:02

I recently came across a blogpost about rewriting an "elegant" function in Clojure into an optimized function in Common Lisp. In it, John Jacobsen discusses how they went from this Clojure code:

and achieved a "nearly 300x" increase in speed. It’s a fun post, finding a better algorithm and diving into the generated assembler and making slow, steady progress on reducing slowdowns.

I took a small, playful offense to the comparison of the unoptimized Clojure and the heavily optimized Common Lisp, but instead of just commenting on it, I decided to try my hand at optimizing the Clojure code, to see what heights can be achieved!

All of my code has been written and run on a 2014 MacBook Pro with a 2.8 GHz Intel Core i7 processor and 16 Gb DDR3 ram. I am running Clojure 1.10.2, javac 1.8.0_292.

That’s 17 full seconds. I don’t need John’s Clojure times to know this is bad, hah. There are some immediate improvements to be made, so let’s dive right in instead of lollygagging here.

Leave a Comment