Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34649

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

References <d6779e35-32b8-417a-abf9-72454573bf56@googlegroups.com> <ka5pfp$ge0$1@reader1.panix.com> <mailman.737.1355254259.29569.python-list@python.org> <ka86ee$kdh$1@rumours.uwaterloo.ca>
Date 2012-12-12 08:34 +1100
Subject Re: String manipulation in python..NEED HELP!!!!
From Tim Delaney <timothy.c.delaney@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.745.1355261688.29569.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 12 December 2012 07:52, Ross Ridge <rridge@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@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

String manipulation in python..NEED HELP!!!! qbailey@ihets.org - 2012-12-10 14:38 -0800
  Re: String manipulation in python..NEED HELP!!!! John Gordon <gordon@panix.com> - 2012-12-10 22:59 +0000
    Re: String manipulation in python..NEED HELP!!!! Terry Reedy <tjreedy@udel.edu> - 2012-12-11 14:30 -0500
      Re: String manipulation in python..NEED HELP!!!! Ross Ridge <rridge@csclub.uwaterloo.ca> - 2012-12-11 15:52 -0500
        Re: String manipulation in python..NEED HELP!!!! Tim Delaney <timothy.c.delaney@gmail.com> - 2012-12-12 08:34 +1100
  Re: String manipulation in python..NEED HELP!!!! Vlastimil Brom <vlastimil.brom@gmail.com> - 2012-12-11 00:25 +0100
  Re: String manipulation in python..NEED HELP!!!! Chris Angelico <rosuav@gmail.com> - 2012-12-11 10:36 +1100
  Re: String manipulation in python..NEED HELP!!!! duncan smith <buzzard@invalid.invalid> - 2012-12-11 16:39 +0000
    Re: String manipulation in python..NEED HELP!!!! Peter Pearson <ppearson@nowhere.invalid> - 2012-12-11 22:21 +0000

csiph-web