On Dec 7, 2007 3:08 PM, Joe Goldthwaite <[EMAIL PROTECTED]> wrote: > Here's the simple benchmark; > > start = time.time() > for x in xrange(3): > for y in xrange(10000000): > pass > print 'xRange %s' % (time.time() - start) > > start = time.time() > for x in range(3): > for y in range(10000000): > pass > print 'Range %s' % (time.time() - start) > > Here's what I get; > > xRange 92.5529999733 > Range 95.2669999599 > > Not a lot of difference. Range is slower but not by much. I know that range > builds > a list then iterates through it. I thought that xrange just kept a counter > that was > incremented and returned with each call. No list was ever created. If that's > true > (and I guess it's not), xrange would be much faster than range. It seems > almost > identical. Given the amount of performance difference, I don't see why > xrange even > exists. >
You can't imagine why someone might prefer an iterative solution over a greedy one? Depending on the conditions, the cost of creating the list can be a greater or a lesser part of the total time spent. Actual iteration is essentially the same cost for both. Try looking at memory usage while you're running these tests. Here's my test results: C:\>python -m timeit "for x in range(10000000):pass" 10 loops, best of 3: 593 msec per loop Memory usage (extremely rough, only for comparison purposes: 163 MB C:\>python -m timeit "for x in xrange(10000000):pass" 10 loops, best of 3: 320 msec per loop Memory usage: just under 4MB You mentioned psyco in your original post, which has specific optimizations for range - I believe it allocates the entire list as an empty memory block, and then creates the integer objects yielded from the range lazily. C:\>python -m timeit "range(10000000)" 10 loops, best of 3: 299 msec per loop C:\>python -m timeit -s "import psyco;psyco.full()" "range(10000000)" 10 loops, best of 3: 0.0376 usec per loop -- http://mail.python.org/mailman/listinfo/python-list