检查元组成员资格的时间复杂度是多少?

What is the time complexity of checking membership in tuples?

在字典、列表和集合中检查成员资格 (x in data_structure) 的时间复杂度列于此处: http://wiki.python.org/moin/TimeComplexity

但是,我在任何 Python 文档中都找不到元组。 我尝试了以下代码来检查自己:

import time 

l = list(range(10000000))
t = tuple(range(10000000))
s = set(range(10000000))

start = time.perf_counter()  
-1 in s
elapsed = time.perf_counter() 
e = elapsed - start 
print("Time spent in set is: ", e)

start = time.perf_counter()  
-1 in l
elapsed = time.perf_counter() 
e = elapsed - start 
print("Time spent in list is: ", e)


start = time.perf_counter()  
-1 in t
elapsed = time.perf_counter() 
e = elapsed - start 
print("Time spent in tuple is: ", e)

我得到这样的结果:

Time spent in set is:  2.0000000000575113e-06
Time spent in list is:  0.07841469999999995
Time spent in tuple is:  0.07896940000000008

这告诉我它也是 O(n)。 谁能证实这一点?是否有官方文档证实这一点?

将元组视为 'frozen list'。一个元组,就像一个列表,必须通过一个条目一个条目地搜索,以便找到一个对象是否是该元组的成员。

成员测试的复杂度与列表相同:O(n)。

dicts 和 sets 是 O(1) 的原因是条目是通过哈希算法访问的。