String manipulation in python..NEED HELP!!!!

Tim Delaney timothy.c.delaney at gmail.com
Tue Dec 11 16:34:46 EST 2012


On 12 December 2012 07:52, Ross Ridge <rridge at csclub.uwaterloo.ca> wrote:

> John Gordon wrote:
> > def encode(plain):
> >     '''Return a substituted version of the plain text.'''
> >     encoded = ''
> >     for ch in plain:
> >        encoded += key[alpha.index(ch)]
> >     return encoded
>
> Terry Reedy  <tjreedy at udel.edu> wrote:
> >The turns an O(n) problem into a slow O(n*n) solution. Much better to
> >build a list of chars and then join them.
>
> There have been much better suggestions in this thread, but John Gordon's
> code above is faster than the equivilent list and join implementation
> with Python 2.6 and Python 3.1 (the newest versions I have handy).
> CPython optimized this case of string concatenation into O(n) back in
> Python 2.4.
>

>From "What's New in Python 2.4":
http://docs.python.org/release/2.4.4/whatsnew/node12.html#SECTION0001210000000000000000

String concatenations in statements of the form s = s + "abc" and s +=
"abc" are now performed more efficiently *in certain circumstances*. This
optimization *won't be present in other Python implementations such as
Jython*, so you shouldn't rely on it; using the join() method of strings is
still recommended when you want to efficiently glue a large number of
strings together.

Emphasis mine.

The optimisation was added to improve the situation for programs that were
already using the anti-pattern of string concatenation, not to encourage
people to use it.

As a real-world case, a bug was recently found in Mercurial where an
operation on Windows was taking orders of magnitudes longer than on
Linux due to use of string concatenation rather than the join idiom (from
~12 seconds spent on string concatenation to effectively zero).

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121212/56b6ab2f/attachment.html>


More information about the Python-list mailing list