Peter Otten wrote: > [EMAIL PROTECTED] wrote: > > > David Murmann wrote: > > >> > # New attempts: > >> > from itertools import imap > >> > def flatten4(x, y): > >> > '''D Murman''' > >> > l = [] > >> > list(imap(l.extend, izip(x, y))) > >> > return l > > >> well, i would really like to take credit for these, but they're > >> not mine ;) (credit goes to Michael Spencer). i especially like > >> flatten4, even if its not as fast as the phenomenally faster > >> flatten7. > >> > > Me too. And expand a bit on flatten4, I got this interesting result. > > > > [EMAIL PROTECTED]:~/bonobo/psp$ python ~/lib/python2.4/timeit.py -s > > "import itertools; a=zip(xrange(1000),xrange(1000))" "l=len(a); > > li=[None]*l*2;li[::2]=range(1000); li[1::2]=range(1000)" > > 1000 loops, best of 3: 318 usec per loop > > > > [EMAIL PROTECTED]:~/bonobo/psp$ python ~/lib/python2.4/timeit.py -s > > "import itertools,psyco; a=zip(xrange(1000),xrange(1000));li=[]" > > "filter(li.extend,a)" > > 1000 loops, best of 3: 474 usec per loop > > For a fair comparison you'd have to time the zip operation. > > > Still 50% slower but it has the advantage that it works on all kinds of > > sequence as they can be efficiently izip() together. > > Creating a list via list/map/filter just for the side effect is not only bad > taste, > > ~ $ python -m timeit -s'a = zip([range(1000)]*2)' 'lst=[];ext=lst.extend' > 'for i in a: ext(i)' > 1000000 loops, best of 3: 1.23 usec per loop > ~ $ python -m timeit -s'a = zip([range(1000)]*2)' 'lst=[];filter(lst.extend, > a)' > 1000000 loops, best of 3: 1.63 usec per loop > > it is also slower than an explicit loop. Don't do it. > Hi, but I found this result instead :
[EMAIL PROTECTED]:~$ python ~/lib/python2.4/timeit.py -s "a=range(10000); b=zip(*[a]*2); l=[None]*len(a)*2; e=l.extend" "l[::2]=b;l[1::2]=b" 100 loops, best of 3: 6.22 msec per loop [EMAIL PROTECTED]:~$ python ~/lib/python2.4/timeit.py -s "a=range(10000); b=zip(*[a]*2); l=[]; e=l.extend" "filter(e,b)" 100 loops, best of 3: 7.25 msec per loop [EMAIL PROTECTED]:~$ python ~/lib/python2.4/timeit.py -s "a=range(10000); b=zip(*[a]*2); l=[]; e=l.extend" "for x in b: e(x)" 100 loops, best of 3: 10.7 msec per loop [EMAIL PROTECTED]:~$ So it seems to be faster than explicit loop. By localizing the l.extend name binding, its speed is only 20% slower than the fastest method. May be I have done something wrong in the test ? -- http://mail.python.org/mailman/listinfo/python-list