why no "do : until"?

Fredrik Lundh fredrik at effbot.org
Tue Jan 9 02:39:46 EST 2001


David Morley wrote:
> This also uses the loop counter rather than constructing an intermediate
> list of indices.  There is the inefficiency of packing and unpacking the
> (i,x) tuples, but I would hope that would be insignificant in Python.

on my machine, your solution is about 5 times slower than

    for i in range(len(a)):
        x = a[i]
        ...

or

    i = 0
    for x in a:
        ...
        i += 1

where a is a built-in sequence type (list, tuple, etc), and its
length is in the 10 to 1,000,000 item range.

(like Aahz, I prefer the second variant.  it's slightly faster on
large sequences, and doesn't depend on len(a) returning the
right thing)

for-indexing should be slightly faster than the second alternative
(less bytecode involved)

Cheers /F





More information about the Python-list mailing list