current rule of thumb for optimization?

2 views
Skip to first unread message

Raoul Duke

unread,
Aug 10, 2009, 6:06:49 PM8/10/09
to clo...@googlegroups.com
hi,

while i realize the real answer is "it depends!", are there any
current rules of thumb based on experience about how to tackle
performance tweaking in Clojure? (e.g. as a small random example, i
think i've heard at times that type notes should speed things up, but
then other times have heard they didn't change things much at all. or
i wonder when/how should transients best be used. etc.)

thanks.

David Nolen

unread,
Aug 10, 2009, 7:07:16 PM8/10/09
to clo...@googlegroups.com
The only thing I've ever seen in Clojure that is "slow" is tight loops on primitives. Most of the time that people bring up Clojure performance, it's usually about tight loops on primitives. For starters, this isn't even something you could really optimize in popular dynamic languages like Python or Ruby.

If you realize your particular application needs fast loops use loop/recur, use type hints (yes they work), be aware which operations get inlined, use Java data structures. There are so many posts on the mailing list now about performance, the answer is just a search away.

Early on I converted a boid simulation from Java to Clojure (the Java version could render each frame in about 5ms, but it was not functional, it used a mutable vector data structure) So I spent a couple of days optimizing my Clojure version, I got it down to about 20ms per frame in Clojure. This was down from 40ms-45ms initially using pure Clojure data structures without type hints.

A few days ago I changed less than 10 lines of code and split the operations across two cores. _10_ lines. Let me repeat, I spent about 30 minutes to get the same level of performance boost that I got spending more than a couple days understanding type hints and primitive array support.  

The rendering time dropped to about 8ms-10ms per frame. That's not 5 ms, but the difference was now just too little for me to care. The Clojure version is simple to maintain, and it's trivial to add logic that simple looks up the number of available cores to split the work, if there are say, 4 or 8 cores.

Perhaps my point is, Clojure, to me, offers more clear and simple routes to optimization than other languages should you need it. While writing parallel computations in most popular languages is a bear, writing it in Clojure is a gleeful stroll in the park. Once you're programming in a functional style, you'll probably discover there are many places where you could write a macro for saying, I have 100,000 things to do, lets just split this into 4 agents, do 25,000 things efficiently on 4 cores, await, and then be on your merry way.

Stuart Sierra

unread,
Aug 10, 2009, 9:51:47 PM8/10/09
to Clojure
First thing to check is always
(set! *warn-on-reflection* true)

Then add type hints until the reflection warnings go away. This can
make 10-20 times difference in code that calls a lot of Java methods.

-SS
Reply all
Reply to author
Forward
0 new messages