小编dea*_*nik的帖子

python 2.7写"x in set"vs"set .__ contains __(x)"

我在python 2.7.6中运行了以下小测试:

s = set(xrange(0, 1000000))
for i in xrange(0, 5000000):
    if s.__contains__(i):
        pass
Run Code Online (Sandbox Code Playgroud)

并获得以下运行输出time python py.py:

real    0m0.616s
Run Code Online (Sandbox Code Playgroud)

然后我将我的代码更改为:

s = set(xrange(0, 1000000))
for i in xrange(0, 5000000):
    if i in s:
        pass
Run Code Online (Sandbox Code Playgroud)

并得到了运行时间0.467s.我也得到了相同的结果dict.我的问题是"为什么存在性能差异?",也许是python如何执行s.__contains__(i)和调用的一些解释i in s

python performance set python-2.7

4
推荐指数
1
解决办法
138
查看次数

标签 统计

performance ×1

python ×1

python-2.7 ×1

set ×1