*apply *is slow. However you can increase performance by 60% with the
following macro, if you have a fixed length in S.
(defmacro *applyn *[n f & s]
(loop [curr `(list* ~@s), n n, vars[] vals[]]
(if(pos? n)
(let[v(gensym)]
(recur v (dec n) (conj(conj vars v) (if (seq vars) (list
'next curr) curr))
(conj vals(list 'first v))))
`(let[~@vars] ~(cons f (seq vals))))))
(let[t(fn[](*apply *+ '(1 2 3 4 5 6 7 8 9 10)))] (time(dotimes [_
1000000] (t)))) ; ~680 msec
(let[t(fn[](*applyn *10 + '(1 2 3 4 5 6 7 8 9 10)))] (time(dotimes [_
1000000] (t)))) ; ~220 msec
So, if you have inner loops, that must be optimized for performance, you
might remember this possibility. Even other functions could be optimized
this way. However, "*premature optimization* is the root of all evil".
Beside, the generated code is more space consuming.
*Marc*
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en