Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

Ned Batchelder ned at nedbatchelder.com
Thu May 12 05:36:24 EDT 2016


On Thursday, May 12, 2016 at 4:24:55 AM UTC-4, srinivas devaki wrote:
> On May 9, 2016 5:31 AM, "Tim Chase" <python.list at tim.thechases.com> wrote:
> >
> > then that's a bad code-smell (you get quadratic behavior as the
> > strings are constantly resized), usually better replaced with
> >
> 
> I just want to point out that in Python s += str in loop is not giving
> quadratic behavior. I don't know why but it runs fast. I'm very much
> interested to know why it is so?
> 
> In [3]: %%timeit
>    ...: s = ''
>    ...: for x in xrange(10**6):
>    ...:     s += str(x)
>    ...:
> 1 loop, best of 3: 383 ms per loop
> 
> In [4]: %%timeit
> s = ''
> for x in xrange(10**6):
>     s = s + str(x)
>    ...:
> 1 loop, best of 3: 383 ms per loop

The CPython optimization depends on the string having only a single
reference.  A seemingly unrelated change to the code can change the
performance significantly:

    In [1]: %%timeit
       ...: s = ""
       ...: for x in xrange(100000):
       ...:   s = s + str(x)
       ...:
    10 loops, best of 3: 33.5 ms per loop

    In [2]: %%timeit
       ...: s = t = ""
       ...: for x in xrange(100000):
       ...:   s = t = s + str(x)
       ...:
    1 loop, best of 3: 1.57 s per loop

Be careful out there...

--Ned.



More information about the Python-list mailing list