【发布时间】:2013-12-12 15:36:16
【问题描述】:
in 运算符在 python 中的速度是否与可迭代的长度成正比?
所以,
len(x) #10
if(a in x): #lets say this takes time A
pass
len(y) #10000
if(a in y): #lets say this takes time B
pass
A > B?
【问题讨论】:
标签: python time size in-operator
in 运算符在 python 中的速度是否与可迭代的长度成正比?
所以,
len(x) #10
if(a in x): #lets say this takes time A
pass
len(y) #10000
if(a in y): #lets say this takes time B
pass
A > B?
【问题讨论】:
标签: python time size in-operator
【讨论】:
__hash__ 方法定义您自己的类。
对此没有通用答案:这取决于a 的类型,尤其是b 的类型。例如,如果b 是一个列表,那么是的,in 采用最坏情况时间O(len(b))。但是,例如,如果 b 是一个 dict 或一个集合,那么 in 会占用预期案例时间 O(1)(即恒定时间)。
关于“Is A > B?”,您没有定义 A 或 B。如上所述,对于您的哪个in 语句会运行得更快,没有一般答案。
【讨论】: