In the API it is suggested to use `seq` to check if coll is empty.

I was working on some code recently found that my biggest performance
bottleneck was calling `seq` to check for emptiness. The calls to
`seq` were causing lots of object allocation and taking noticeable CPU
time. I switched to using `identical?` to explicitly compare against
the empty vector and was rewarded with a drastic reduction in
execution time.

Here are some hasty tests showing just how big the difference can be:

user=> (let [iterations 100000000] (time (dotimes [_ iterations]
(identical? [] []))) (time (dotimes [_ iterations] (seq []))))
"Elapsed time: 3.512 msecs"
"Elapsed time: 2512.366 msecs"
nil
user=> (let [iterations 100000000] (time (dotimes [_ iterations]
(identical? "" ""))) (time (dotimes [_ iterations] (seq ""))))
"Elapsed time: 3.898 msecs"
"Elapsed time: 5607.865 msecs"
nil
user=> (let [iterations 100000000] (time (dotimes [_ iterations]
(identical? () ()))) (time (dotimes [_ iterations] (seq ()))))
"Elapsed time: 3.768 msecs"
"Elapsed time: 2258.095 msecs"
nil

Has any thought been given to providing a faster `empty?` that is not
based on seq?

Thanks,
Eric Kobrin

-- 
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