最近我开始使用Python3,它缺乏xrange的伤害.
简单的例子:
1) Python2:
from time import time as t
def count():
st = t()
[x for x in xrange(10000000) if x%4 == 0]
et = t()
print et-st
count()
Run Code Online (Sandbox Code Playgroud)
2) Python3:
from time import time as t
def xrange(x):
return iter(range(x))
def count():
st = t()
[x for x in xrange(10000000) if x%4 == 0]
et = t()
print (et-st)
count()
Run Code Online (Sandbox Code Playgroud)
结果分别是:
1) 1.53888392448 2) 3.215819835662842
这是为什么?我的意思是,为什么xrange被删除了?这是一个很好的学习工具.对于初学者,就像我一样,就像我们所有人一样.为什么删除它?有人能指出我正确的PEP,我找不到它.
干杯.