Efficient string concatenation methods
Terry Reedy
tjreedy at udel.edu
Sun May 2 10:49:01 EDT 2004
> Oliver Crow <ocrow at skymind.com> wrote:
> > I found several sources with advice for how to do concatenation in a
> > pythonic way (e.g. ref#1), but I hadn't seen any measurements or
> > comparisons. So, I put together a little test case and ran it through
> > for six different methods. Here's the results, and my conclusions:
> >
> > http://www.skymind.com/~ocrow/python_string/
> >
> > I'd be happy to hear if anyone else has done similar tests and if
> > there are any other good candidate methods that I missed.
This
def method4():
str_list = []
for num in xrange(loop_count):
str_list.append(`num`)
return ''.join(str_list)
will run slightly faster modified to this:
def method4():
str_list = []
append = str_list.append
for num in xrange(loop_count):
append(`num`)
return ''.join(str_list)
by factoring the method lookup out of the loop. Ditto for 3 and 5.
Terry J. Reedy
PS. Changing IE's View/TextSize changes size of header fonts but not body
text, which your CSS apparently fixes at a size a bit small for me on
current system.
More information about the Python-list
mailing list