Loop performance disappearance
François Pinard
pinard at iro.umontreal.ca
Wed Mar 15 10:24:06 EST 2000
Mikael Johansson <mikael.johansson at helsinki.fet> writes:
> for i in range(loops):
> pass
> If loops=500 000 (space added for clarity) the "program" executes in ~2
> secs on my machine. But if loops is set to 5 000 000, the execution time
> rises to substantially more than tenfold (I terminated it after one
> minute). However the CPU-load is quite small, most of the time is spent
> disk swapping like crazy!
> Any ideas of the reason for this, work-arounds?
Sure. range(N) does allocate a list containing integers from 0 to N-1.
So, if you allocate a huge list, more than the physical memory can hold,
you are likely to need swapping space.
The work-around is to use xrange instead of range. It yields a kind of
lazy-evaluated tuple, consuming almost no memory. I would guess that
range is to be preferred over xrange for when N is not very big.
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list