Chapter 7Performance

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

—Donald Knuth

In this chapter you will implement tools and techniques to make code execute faster. How fast can a dynamic, lazy, functional, immutable language be, you ask. Pretty fast. You can expect Clojure code to take around 2–5 times the amount of CPU of equivalent Java code. With a little work you can tweak it to be as fast as Java code. In turn, you can expect Java code to perform at around 1.2 times C. This is amazing considering the high-level abstractions you get to leverage from Clojure. Compared to dynamic languages like Ruby, Clojure is blazingly fast.

So, how does Clojure achieve such great performance? The answer is the JVM. Clojure embraces its host platform and relies on its mature byte code optimization. The same holds true for ClojureScript (relies on the Closure Compiler) and ClojureCLR (embraces the .NET runtime). There is an intentional tradeoff made to embrace the underlying platform and not completely abstract it away. The benefit is performance and interoperability. The downside is you have to live with some unsightly details of the underlying ...

Get Professional Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.