[triangle-zpug] why is this using a lot of memory?

J. Cliff Dyer jcd at sdf.lonestar.org
Tue Mar 25 16:33:32 CET 2008


range(x,y) creates a list of all the numbers between x and y.  For
sufficiently large y (and decently small x), you will run out of
memory.  

Try xrange(x,y), which creates an iterator, which yields each number in
turn.

Cheers,
Cliff

On Tue, 2008-03-25 at 08:23 -0700, Joseph Mack NA3T wrote:
> intervals=10000000 #some large number
> height=0
> for x in range(0, intervals):
>  	height+=x
> 
> (I get the same result whether running interactive or from a 
> file with the above code)
> 
> For a sufficiently large value of "intervals" the program 
> exits immediately with a MemoryError. For slightly smaller 
> values, the program takes up most of the memory of the 
> machine. I would have thought that you'd only be allocating 
> a couple of variables. What am I missing? Am I allocating an 
> "intervals" number of something, rather than a single 
> variable with a large value?
> 
> thanks Joe
> 





More information about the TriZPUG mailing list