efficiency of range() and xrange() in for loops

Fredrik Lundh fredrik at pythonware.com
Thu Apr 6 01:32:03 EDT 2006


Steve R. Hastings wrote:

> > the difference isn't very
> > large, xrange is actually slower in some python versions, and you'll
> > need the integer objects sooner or later anyway...
>
> Actually, for many uses of "for i in (range|xrange)", you only need the
> value of i, and you aren't doing anything with the integer object.

so you're saying that the value of i isn't an integer object?

if you don't need the index value at all, itertools.repeat is sometimes
faster than xrange.

> for i in range(10**6):
>     pass
>
> and produced code equivalent to this:
>
> for i in iter(range(10**6))
>    pass
>
> How would this break stuff?

how would that speed anything up?

</F>






More information about the Python-list mailing list