Which is faster and why? Set or List?

前端 未结 3 2051
误落风尘
误落风尘 2020-11-30 10:09

Lets say that I have a graph and want to see if b in N[a]. Which is the faster implementation and why?

a, b = range(2)
N = [set([b]), set([a,b])         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 10:31

    Membership testing in a set is vastly faster, especially for large sets. That is because the set uses a hash function to map to a bucket. Since Python implementations automatically resize that hash table, the speed can be constant (O(1)) no matter the size of the set (assuming the hash function is sufficiently good).

    In contrast, to evaluate whether an object is a member of a list, Python has to compare every single member for equality, i.e. the test is O(n).

提交回复
热议问题