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


Groups > comp.lang.python > #108547

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

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?
Date 2016-05-12 18:40 +1000
Message-ID <mailman.605.1463042424.32212.python-list@python.org> (permalink)
References <ngogql$bgm$1@dont-email.me> <20160508183738.2f551043@bigbox.christie.dr> <CACs7g=CwX1i7WxMUubrZqY3ODNkJoZ4W-ObZ=vS7koVzm857Ww@mail.gmail.com> <CAPTjJmqf4bUPHWtxRRLdanfOEd-iLgHjW5-qU0SvjWfrWanE0Q@mail.gmail.com>

Show all headers | View raw


On Thu, May 12, 2016 at 6:23 PM, srinivas devaki
<mr.eightnoteight@gmail.com> wrote:
> On May 9, 2016 5:31 AM, "Tim Chase" <python.list@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

Some versions of CPython do include an optimization for this. However,
it's not guaranteed, and it's easy to disrupt. For starters, it works
only if you're appending to a string, not prepending; this will be
much slower:

s = ''
for x in range(10**6):
    s = str(x) + s

And other Pythons may not optimize this. So don't depend on it.

ChrisA

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


Thread

Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-08 19:10 -0400
  Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Tim Chase <python.list@tim.thechases.com> - 2016-05-08 18:37 -0500
  Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Joel Goldstick <joel.goldstick@gmail.com> - 2016-05-08 20:02 -0400
  String concatenation (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-09 02:44 +0200
    Re: String concatenation (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?) Chris Angelico <rosuav@gmail.com> - 2016-05-09 12:16 +1000
      Re: String concatenation Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-09 23:21 +0200
        Re: String concatenation Steven D'Aprano <steve@pearwood.info> - 2016-05-10 12:32 +1000
          Re: String concatenation Chris Angelico <rosuav@gmail.com> - 2016-05-10 12:42 +1000
            Re: String concatenation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-12 17:43 +1000
        Re: String concatenation Rustom Mody <rustompmody@gmail.com> - 2016-05-09 20:45 -0700
          Re: String concatenation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-10 16:46 +1000
            Re: String concatenation Paul Rubin <no.email@nospam.invalid> - 2016-05-10 00:13 -0700
              Re: String concatenation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-10 17:54 +1000
                Re: String concatenation David Palao <dpalao.python@gmail.com> - 2016-05-10 10:15 +0200
                Re: String concatenation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-12 17:27 +1000
    Re: String concatenation (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?) srinivas devaki <mr.eightnoteight@gmail.com> - 2016-05-09 09:01 +0530
    Re: String concatenation (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?) srinivas devaki <mr.eightnoteight@gmail.com> - 2016-05-09 09:13 +0530
    Re: String concatenation (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?) Michael Selik <michael.selik@gmail.com> - 2016-05-09 18:29 +0000
    Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-10 19:53 -0400
      Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-11 02:16 +0200
        Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-10 20:33 -0400
        Moderation [was Re: ...What's wrong with this concatenation statement?] Steven D'Aprano <steve@pearwood.info> - 2016-05-11 11:17 +1000
        Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Chris Angelico <rosuav@gmail.com> - 2016-05-11 11:43 +1000
          Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-11 21:12 +0200
        Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Ned Batchelder <ned@nedbatchelder.com> - 2016-05-11 12:39 -0700
          Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Chris Angelico <rosuav@gmail.com> - 2016-05-12 09:07 +1000
          Re: % formatting vs .format() (was: What's wrong with this concatenation statement?) Tim Chase <python.list@tim.thechases.com> - 2016-05-11 18:31 -0500
    Re: String concatenation (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?) sohcahtoa82@gmail.com - 2016-05-10 17:38 -0700
      Re: String concatenation Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-05-11 21:14 +0200
        Re: String concatenation sohcahtoa82@gmail.com - 2016-05-11 14:30 -0700
          Re: String concatenation Ned Batchelder <ned@nedbatchelder.com> - 2016-05-11 14:50 -0700
          Re: String concatenation DFS <nospam@dfs.com> - 2016-05-11 18:25 -0400
  Re: What's wrong with this concatenation statement? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-09 17:53 +1000
    Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-10 12:16 -0400
      Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Rustom Mody <rustompmody@gmail.com> - 2016-05-10 09:41 -0700
      Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Stephen Hansen <me+python@ixokai.io> - 2016-05-10 09:42 -0700
        Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-10 18:37 -0400
      Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Steven D'Aprano <steve@pearwood.info> - 2016-05-11 02:44 +1000
      Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Ian Kelly <ian.g.kelly@gmail.com> - 2016-05-10 12:15 -0600
        Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-10 17:21 -0400
          Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Chris Angelico <rosuav@gmail.com> - 2016-05-11 07:27 +1000
            Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-10 19:40 -0400
              Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Paul Rubin <no.email@nospam.invalid> - 2016-05-10 19:14 -0700
              Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? alister <alister.ware@ntlworld.com> - 2016-05-12 09:12 +0000
                Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Chris Angelico <rosuav@gmail.com> - 2016-05-12 19:23 +1000
                Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-12 08:24 -0400
                Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Michael Torrie <torriem@gmail.com> - 2016-05-12 08:39 -0600
                Why online forums have bad behaviour (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?) Ben Finney <ben+python@benfinney.id.au> - 2016-05-13 05:36 +1000
                Re: Why online forums have bad behaviour (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?) Jason Friedman <jsf80238@gmail.com> - 2016-05-12 21:05 -0600
          Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Ethan Furman <ethan@stoneleaf.us> - 2016-05-10 15:12 -0700
            Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-10 19:29 -0400
              Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-05-12 13:39 +0000
                Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Chris Angelico <rosuav@gmail.com> - 2016-05-12 23:50 +1000
          Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Ethan Furman <ethan@stoneleaf.us> - 2016-05-10 16:16 -0700
            Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? DFS <nospam@dfs.com> - 2016-05-10 19:41 -0400
  Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? srinivas devaki <mr.eightnoteight@gmail.com> - 2016-05-12 13:53 +0530
    Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Ned Batchelder <ned@nedbatchelder.com> - 2016-05-12 02:36 -0700
      Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Steven D'Aprano <steve@pearwood.info> - 2016-05-13 01:06 +1000
  Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Chris Angelico <rosuav@gmail.com> - 2016-05-12 18:40 +1000

csiph-web