Larry Bates <la*********@websafe.comwrote:
Note: if lists are long take a look at itertools izip. zip creates
a list of lists which could take lots of memory/time if they are VERY
large. itertools izip iterates over them in place.
That's interesting. I was going to quibble with the assertion that zip
could take lots of time, since on the face of it a loop using izip packs
and unpacks just as many tuples. Fortunately I tried it out before claiming
that zip would be just as fast :)
It would appear that even for short sequences the izip solution is faster.
My guess would be it is because the same tuple objects are being reused in
the izip version.
C:\Python25\Lib>timeit.py -s "a, b, c = [range(1000)]*3" -s "from itertools
import izip" "for p,q,r in izip(a,b,c): pass"
10000 loops, best of 3: 131 usec per loop
C:\Python25\Lib>timeit.py -s "a, b, c = [range(1000)]*3" "for p,q,r in
zip(a,b,c): pass"
1000 loops, best of 3: 212 usec per loop
C:\Python25\Lib>timeit.py -s "a, b, c = [range(100)]*3" -s "from itertools
import izip" "for p,q,r in izip(a,b,c): pass"
100000 loops, best of 3: 13.9 usec per loop
C:\Python25\Lib>timeit.py -s "a, b, c = [range(100)]*3" "for p,q,r in
zip(a,b,c): pass"
10000 loops, best of 3: 22.6 usec per loop
C:\Python25\Lib>timeit.py -s "a, b, c = [range(10)]*3" -s "from itertools
import izip" "for p,q,r in izip(a,b,c): pass"
100000 loops, best of 3: 2.21 usec per loop
C:\Python25\Lib>timeit.py -s "a, b, c = [range(10)]*3" "for p,q,r in
zip(a,b,c): pass"
100000 loops, best of 3: 3.52 usec per loop