1 | initial version |
OK, that makes more sense. But for some purposes, xmrange is faster. Suppose you actually want the output of xmrange as a list. Then if you look at:
def test1(nx):
return xmrange([nx]*3)
def test2(nx):
answer = []
for k1 in xrange(nx):
for k2 in xrange(nx):
for k3 in xrange(nx): answer.append([k1,k2,k3])
return answer
the xmrange solution wins:
timeit('test1(10)')
625 loops, best of 3: 28.5 µs per loop
timeit('test2(10)')
625 loops, best of 3: 403 µs per loop
although I'm sure there are more efficient ways to use xrange for this.