Making a dict from two lists/tuples

Alex Martelli aleaxit at yahoo.com
Thu May 24 10:13:57 EDT 2001


"Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in message
news:Xns90AB8F1F0AA53duncanrcpcouk at 127.0.0.1...
> "Alex Martelli" <aleaxit at yahoo.com> wrote in
> news:9eip7i0s67 at enews1.newsguy.com:
>
> > The fancy/neat form seems to be better than
> > three times faster on my box, so it may be
> > worth using DESPITE its fanciness...:-).
>
> If you bump up the ranges to 100000 the difference increases to a factor
of
> eight or better.

Not on my box -- here are the times with 100,000:

D:\Python21>python po.py
Plain: 1.80506913447
Fancy: 0.476084956025
Emile: 0.619002800914

D:\Python21>python po.py
Plain: 1.77454571055
Fancy: 0.478982250822
Emile: 0.627446609151

but bumping it up to a cool million does show this
kind of effect:

D:\Python21>python po.py
Plain: 56.9542869403
Fancy: 4.5616016668
Emile: 6.02709980539

D:\Python21>python po.py
Plain: 55.9481093412
Fancy: 4.53220380462
Emile: 6.17755044914


The fancy map, and Emile's simple and clear loop-on-index,
are scaling up more or less linearly, while zip clearly
starts having serious problems building the million tuples
needed and the million-long list to hold them.  Maybe some
cool iterator will help in the future:-).

> Definitely worth using, provided you have first profiled the code and know
> that it is a bottleneck.

As per my previous post in response to Emile, I don't think
the normal "first profile &c" approach is quite suitable to
a technique that qualifies as "an idiom".  I don't wait to
profile &c before I code a case of "build up a big string
from many little pieces" in terms of a list (with .append,
or a comprehension, or...) and a final ''.join rather than
the most straightforward "loop-on-string +=" -- I've learned
once and for all that the latter is just too slow for real
use, so I automatically use the "fancy" idiom instead.  As
I use it often, gradually I stop perceiving it as "fancy";
it becomes, for me, THE "obviously correct" way to approach
a certain frequent category of tasks in Python...


Alex






More information about the Python-list mailing list