xrange question

John Flynn transpicio at yahoo.com.au
Mon May 7 00:29:08 EDT 2001


"Carlos Ribeiro" <cribeiro at mail.inet.com.br> wrote in message
news:mailman.989172369.2104.python-list at python.org...

[ good explanation of xrange ]

> So why can't we use the while statement to solve the same problem, as you
> suggested?
>
> x= 0
> while x < 1000:
>      x = x + 1
>      do_something(x)
>
> Try this compared to the for loop, and you'll be surprised by the speed
> loss. Why it is so? Because the explicit increment is done by the
> interpreted code, that is slower than the increment done inside the Python
> library - that's C code, running natively.

Ah, that's what I was missing. I assumed a while loop would be faster than
xrange without actually testing it.

> I hope that this does clarify things for you...

Yes it does, thanks.






More information about the Python-list mailing list