building strings from variables

Gal Diskin gal.diskin at gmail.com
Sat Oct 7 09:46:58 EDT 2006


First of all - thanks for all the replies. (Sorry for my slowness in
answering, but I wrote this message from work, just before leaving for
the weekend.)

I found a couple of posts that might be of interest:
Regarding speed I found a similar discussion in this newsgroup with
interesting results:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/86aa0dd9dc0ed9cd/502a252f2c5ed778
Wesley, you might be interested to find that join is not necessarily
faster according to this thread. Jordan you might also be interested to
see their benchmarking.
The following post may also be of interest:
http://groups.google.com/group/comp.lang.python/msg/5ca4321e1d493fe9
(I haven't found the time to check this one myself so it comes with
limited warranty)

Regarding readability and "better" code my preferred methods are the 2
methods I haven't mentioned introduced by Rainy (%(varname)s ..." %
globals()) and by Scott David Daniels (' '.join[...]). Thanks, I
already knew join, but failed to include it for some reason but I
wasn't aware you could use dictionaries like this as well.

Thanks Jordan for making the effort to do the benchmarking. I think I
got similar results on several machines - according to my personal
benchmarking (based on short strings, and therefore relevant only to
such), the fastest method is "%s %d %s" % (p1, p2, p3) . However, all
other methods get close results (i.e. not above 1.5x runtime) except
for template.replace which is much slower by rate of about 15x. When it
comes to choosing between using template.replace or "%(varname)s ..." %
globals() my preference is for the latter without a doubt.

--
Gal Diskin
G__.D_____ at Intel.com
G__.D_____ at gmail.com
Work: +972-4-865-1637
Cell:   +972-54-7594166




More information about the Python-list mailing list