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

srinivas devaki mr.eightnoteight at gmail.com
Thu May 12 04:23:55 EDT 2016


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



More information about the Python-list mailing list