On Sun, Nov 14, 2010 at 2:16 PM, Eric Kobrin <erl...@gmail.com> wrote:
> 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.

Only there's a wee problem:

user=> (identical? [] (pop [3]))
false

Not all empty vectors are considered identical. = works:

user=> (= [] (pop [3]))
true

= is also ten times SLOWER than seq if the empty vectors are not
identical, though it's as fast as identical? when they are:

user=> (let [iterations 100000000] (time (dotimes [_ iterations]
(= [] (pop [3])))) (time (dotimes [_ iterations] (seq []))))
"Elapsed time: 29994.93852 msecs"
"Elapsed time: 2745.21924 msecs"

This is troubling. The seq function should be faster than this, and =
on nonidentical empty colls should be WAY faster than this.

On the other hand, the identical? test taking <4ms for 100,000,000
iterations suggests it was optimized away to nothing by the JIT
compiler. Unless of course both of us have 100GHz CPUs that can
execute a fresh instruction every 10 picoseconds. :)

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