map del efficiency python2.2 and 2.1
Bengt Richter
bokr at oz.net
Wed Jul 17 05:04:55 EDT 2002
On Wed, 17 Jul 2002 07:32:16 GMT, Alex Martelli <aleax at aleax.it> wrote:
>Bengt Richter wrote:
>
>> On Tue, 16 Jul 2002 15:53:43 GMT, Alex Martelli <aleax at aleax.it> wrote:
>>
>>>John Hunter wrote:
>>> ...
>>>> def pairs(l):
>>>> return [(l[i], l[i+1]) for i in range(0, len(l), 2)]
>> ^^^^^
> ...
>>>def pairs(L):
>>> for i in xrange(0, len(L), 2):
>> ^^^^^^
>> ^
>>> yield L[i], L[i+1]
>>>
>>>but that's a minor issue, I guess.
>>>
>>>Another likely performance boost in 2.2 would be:
>>>
>>>m = dict(pairs(seq))
>>>
>>>with pairs coded as here shown.
>>
>> In case John didn't notice, that little 'x' your finger likely
>> typed automatically probably made some difference too ;-)
>
>Yep, but John had used xrange in other spots of his code,
>so I didn't think I needed to explain better. Still, that
>may have been an error on my part. So, the better explanation:
>range and list comprehensions build lists and thus may have
>to allocate lots of memory. xrange and generators don't build
>any list but rather return one item at a time as needed.
>
>But the importance of using xrange vs range is minor -- yes
>it does make SOME difference, but it's small. See:
>
>from __future__ import generators
>import time, gc
>
>L = range(300 * 1000)
>
>def timit(f):
> start = time.clock()
> m = dict(f(L))
> stend = time.clock()
> print f.__name__, stend-start
>
>def pairs_lc_ra(L):
> return [ (L[i],L[i+1]) for i in range(0, len(L), 2) ]
>
>def pairs_lc_xr(L):
> return [ (L[i],L[i+1]) for i in xrange(0, len(L), 2) ]
>
>def pairs_ge_ra(L):
> for i in range(0, len(L), 2):
> yield L[i], L[i+1]
>
>def pairs_ge_xr(L):
> for i in range(0, len(L), 2):
^-- needs x, but doesn't make much difference
> yield L[i], L[i+1]
>
>gc.disable()
>
>for i in range(3):
> for f in pairs_lc_ra, pairs_lc_xr, pairs_ge_ra, pairs_ge_xr:
> timit(f)
>
>
>[alex at lancelot MyDocuments]$ python pa.py
>pairs_lc_ra 0.53
>pairs_lc_xr 0.52
>pairs_ge_ra 0.29
>pairs_ge_xr 0.28
>pairs_lc_ra 0.52
>pairs_lc_xr 0.52
>pairs_ge_ra 0.28
>pairs_ge_xr 0.28
>pairs_lc_ra 0.52
>pairs_lc_xr 0.52
>pairs_ge_ra 0.28
>pairs_ge_xr 0.28
>
>This is with 2.2.1. 2.3 built from CVS lowers generator's advantage:
>
>[alex at lancelot src]$ ./python pa.py
>pairs_lc_ra 0.44
>pairs_lc_xr 0.4
>pairs_ge_ra 0.29
>pairs_ge_xr 0.28
>pairs_lc_ra 0.43
>pairs_lc_xr 0.4
>pairs_ge_ra 0.29
>pairs_ge_xr 0.29
>pairs_lc_ra 0.43
>pairs_lc_xr 0.39
>pairs_ge_ra 0.29
>pairs_ge_xr 0.29
>
>but even here, using generators rather than list comprehensions
>seems still much more important than using xrange rather than range.
>
Yes. Looks like about ~5% advantage for 'x' and ~100% for 'ge' on my
old machine (P2-300mhz/320MB with
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32):
pairs_lc_ra 2.12676607592
pairs_lc_xr 2.02671428164
pairs_ge_ra 1.00829043683
pairs_ge_xr 0.942929913458
pairs_lc_ra 2.11218992576
pairs_lc_xr 2.07914132127
pairs_ge_ra 1.00314620904
pairs_ge_xr 0.959043968146
pairs_lc_ra 2.0945639475
pairs_lc_xr 2.22773139387
pairs_ge_ra 1.03221134747
pairs_ge_xr 0.952561302467
Thanks for the demo.
Regards,
Bengt Richter
More information about the Python-list
mailing list