On Oct 12, 2010, at 3:02 PM, tonyl wrote:

> (defn palindrome? [s]
>  (= s (reduce str (reverse s))))

One opportunity to micro-optimize is to replace "reduce str" with "apply str". 
str uses a StringBuilder object to build a string from its arguments. When you 
use reduce, the code walks down s appending the result so far with the next 
string, creating a new StringBuilder object each time. When you use apply, str 
creates one StringBuilder object and passes all the remaining elements of s to 
it.

The amount of improvement depends on the length of s. In a micro-benchmark I 
did just now, using apply reduced the overall runtime of the benchmark by a 
factor of 4.8. (pun acknowledged).

--Steve

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to