Reading this question, I wondered how much time (asymptotically speaking) does it takes to Python to evaluate expressions like
{1,2}=={2,1}
that is to say, to check if two instances of the set class are equal.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Comparison between sets is implemented by the function
set_richcompareinsetobject.c, line 1848. You’ll see that equality is implemented as follows:If the sets do not have the same size, return false.
If both sets have been hashed, and the hashes differ, return false.
Call
set_issubset.The subset test for two sets looks like this:
You’ll see that it works by iterating over all the elements of the first set and then looking up each one in the other set. So (unless there are a lot of hash collisions) this is linear in the size of the first set.