Hi Mark,

The results will depend on the objects you are comparing.  If you need
to search through the list multiple times, converting to a set once is
almost certainly going to be faster.  But, if you're just doing it
once, iterating will usually be much faster:

user> (time (dotimes [_ 100000] (contains? (set (range 100)) 10)))
"Elapsed time: 7708.336 msecs"

user> (time (dotimes [_ 100000] (some #(= 10 %) (range 100))))
"Elapsed time: 291.474 msecs"

user> (time (let [s (set (range 100))] (dotimes [_ 100000] (contains?
s 10))))
"Elapsed time: 27.978 msecs"

In a simple test I get similar results for strings too... what
conditions were you testing under?

-Jason

On Feb 26, 7:53 am, Mark Volkmann <r.mark.volkm...@gmail.com> wrote:
> I thought for sure it would be faster to use "some" to determine
> whether an item was in a list rather than convert the list to a set
> and then use "contains?", but it turns out I was wrong. The latter
> approach takes about 1/3 to 1/2 the time! This is the case even when
> the list contains 100 items. Of course I realize that if this is a
> commonly needed operation for a particular collection, it's better to
> use a set instead of a list in the first place.
>
> (def stooges (list "Moe" "Larry" "Curly"))
> (time (some #(= % "Curly") stooges))
> (time (contains? (set stooges) "Curly"))
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
--~--~---------~--~----~------------~-------~--~----~
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
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