map del efficiency python2.2 and 2.1

Bengt Richter bokr at oz.net
Tue Jul 16 23:12:21 EDT 2002


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)]
                                       ^^^^^
>
>In 2.2, with "from __future__ import generators" at the start
>of your module, you can probably use this almost as fast as
>the other, less-elegant solution, by recoding it as a generator:
>
>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 ;-)

>
>Regarding del time, you could measure that directly by
>doing a "del m" and seeing times before and after; also,
>just in case (shouldn't matter, but...), you could try
>gc.disable() and see if that makes a difference.
>
The range(0, len(l), 2) would have been part of the garbage
to collect too, unless gc had already hit (in which case
gc.disable() might change timing in two places).

BTW, I wonder what the speed difference is in collecting
deleted numbers in the magic shared range(-1,100) vs others is,
given that it would just be decrementing refs until the end.

Regards,
Bengt Richter



More information about the Python-list mailing list