problems caused by very large for-loop

Delaney, Timothy (Tim) tdelaney at avaya.com
Thu Dec 7 20:34:31 EST 2006


sam wrote:

> so far so good, but i was using 'for i in the range(iterations):' a
> for loop over each slice of time, where the number of iterations was
> getting into the tens of millions. up until about 125,000,000 it
> worked, then i got a MemoryError.

Your analysis was correct - range() returns a list containing all the
integers, which in this case would chew up a *very* large chunk of
memory.

Even worse, because all the integers are instantiated at once, that very
large chunk of memory would not be made available for anything except
integers (due to the way Python allocates memory for integers).

The canonical way to deal with this is to use xrange().

    for i in xrange(iterations):

This simply maintains an internal counter, rather than instantiating a
list of all the integers.

Tim Delaney



More information about the Python-list mailing list