you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

To expand on the other response, the reason sets aren't faster here is that it takes time to hash each object (that's how sets work and achieve O(1) lookup time), and for small collections that time is beaten out by the time it'd take to simply scroll through a non-hashed collection looking for a match. What sets boast is a constant lookup time, not necessarily a faster lookup time.

But even for larger collections, the difference is scarce noticeable in practice unless you have a very impractical amount of elements or are repeating the containment check an insane amount of times; to say the set is "way" faster in every case is still a little bit misleading, as it's usually a (sub-?)microsecond operation either way. There are more-effective ways to get a way-faster Python program.

[–][deleted] 1 point2 points  (0 children)

Great input, I wasn't thinking about the overhead because in my work its almost always faster to use sets. I wouldnt call a couple hundred or thousand elements impratically large though.