xrange questioin

Fredrik Lundh fredrik at pythonware.com
Sun Jul 28 03:42:04 EDT 2002


Bengt Richter wrote:

> >> What is better: for x in range(n) or for x in xrange(n)?
> >> (with n is relatively small, like 10 or 100)
> >
> >for in range() is usually a few percent faster, but if you do some-
> >thing inside the loop, you'll hardly notice the difference.
>
> But speed isn't the only thing. Range builds a potentially
> huge list in membory.

not if "n is relatively small, like 10 or 100".

in CPython, for values up to 100, it builds a list of pointers to
an array of preallocated integers.  that's just over 400 bytes
on most platforms, which is far from huge.

> If we didn't have range

but we do.

what's wrong with just answering the original question?  does
every answer these days has to contain an essay on some un-
related topic, or a sales pitch for the author's favourite library?
(that's what blogs and sigs are for, really).

</F>





More information about the Python-list mailing list