Reputation: 512
I have two large integer sets, set1
and set2
. Which one of the below is more efficient?
Example:
if(set1 == set2)
or
if(len(set1)==len(set2))
Upvotes: 4
Views: 78727
Reputation: 1498
The two statements are completely different from each other.
if(set1==set2)
compares for equality of each element in both the sets, and evaluates to true if and only if both the sets are exactly same.
Whereas if(len(set1)==len(set2))
compares only the length of both the sets.
Even if you have two sets with same length there may be cases when they are not the same. For eg consider this:
set1: [1, 3, 6, 29, 31]
set2: [1, 3, 7, 10, 15]
Though the sets have the same length they are not the same.
You could do this to save on time.
if len(set1) == len(set2):
if set1 == set2:
//TODO when sets are equal
else
//TODO when sets are not equal.
else
//TODO when sets are not equal
Upvotes: 33
Reputation: 5114
As Nikhil Wagh already explained, in the original question there was a confusion between comparing the length of a set and checking the equality of two sets. I just want to add that simple s1 == s2
is enough and is efficient, because __eq__() (and its oppsite __ne__()) method already performs this optimization. It first checks if the sets have the same length, then it checks if the sets have the same hash, and only then it performs set_issubset
check.
You can find the implementation of set equality check in function set_richcompare()
in file setobject.c
. You don't need to be an expect in C to understand the algorithm - https://github.com/python/cpython/blob/master/Objects/setobject.c
Upvotes: 25