Path: csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Date: Thu, 12 May 2016 18:40:22 +1000 Lines: 38 Message-ID: References: <20160508183738.2f551043@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de T3imzaFCENg989b0o1DugQcyj9NW3dryO+LpxgpDaq8Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cpython': 0.05; 'so?': 0.07; 'subject:skip:c 10': 0.07; 'cc:addr:python-list': 0.09; '[3]:': 0.09; 'python': 0.10; 'subject: \n ': 0.15; 'thu,': 0.15; '2016': 0.16; '[4]:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'quadratic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'string,': 0.18; 'runs': 0.18; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'am,': 0.23; '(you': 0.23; 'header:In- Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'this.': 0.28; 'appending': 0.29; 'fast.': 0.29; 'loop,': 0.29; 'str': 0.29; "i'm": 0.30; 'point': 0.33; 'usually': 0.33; 'optimize': 0.33; 'received:google.com': 0.35; 'replaced': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'received:209': 0.38; 'why': 0.39; 'subject:the': 0.39; 'skip:x 10': 0.40; 'subject:with': 0.40; 'some': 0.40; 'easy': 0.60; 'behavior': 0.61; 'per': 0.62; 'chrisa': 0.84; 'starters,': 0.84; 'subject:this': 0.85; 'subject:you': 0.85; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=r370UZhCf/30ecxNs/y1IbBL5o5tYpe4GIhboEi8cLQ=; b=SdOLd2XgBby8Pv1c52Qe1hctjKG1pWs9BtfTvFStZ0KX/YxxWbukzExaYrroN0ezsB 2Ne7Fw7ThuuKNsaSdFj5eNuE2Ha2w1OYhYelWPx7zRFozMa45QLDZB4teJESj1kKl7yi 4VKkyA+1aXAAGx3eAsMxMDNe56gfDz2Cx+71FDFnCao0Kbmq0SM88i1ttZwkMHDUHA1u +B+inJxHfvOrtGeeABtp1CtG0jx1z/i2q6gaskJKPhhR2Eog7bUKtsYU/LlLWuNp4PPh kKo96O4tKAP0yVooeNvmAyMigRIbCZ10pfRxBisk+0m2UqINDdj34lgpdNaXoHqZzLIN /hnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=r370UZhCf/30ecxNs/y1IbBL5o5tYpe4GIhboEi8cLQ=; b=jGkyGomeW7oTXAriIdY2pNxGEPLjH0HEMAUPVtRcIvrmHojxI4XTF9Yju0WWf9BiPs gVOOL5ErNcmXf8Fym3+eX5lLDJ/EgcV0mGPQ7CI+rZPxsMZwOpVk35A81TRC5T94GmkG O0OmPrHl8p1OZ1a79pvCSkbkY6Ay5kZpIiZZL5brCyA8bcCdQKAoLUuePwUqlxo7erGp qDptBu+NRK5RRcQLqKBgfjwaLyhtBg7Dmzfct02IMjT/iVqamwHJoutlLvOKyqFYyG7N lB5vgI/3UnvFd+xhxG7jo7bqoPXUZKEjbPXzQSWOTdAfJbjFPfgz6MzqA3LVYktzzeWY WVdA== X-Gm-Message-State: AOPr4FXe/+9ac3sJ8plLWEbNMtUombvnSVZ8gjfAG0YFoFaxkRENGXD122ywfXbj2B6AkyfceMUjTzbHpA+2IA== X-Received: by 10.112.189.1 with SMTP id ge1mr3780255lbc.9.1463042422479; Thu, 12 May 2016 01:40:22 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <20160508183738.2f551043@bigbox.christie.dr> Xref: csiph.com comp.lang.python:108547 On Thu, May 12, 2016 at 6:23 PM, srinivas devaki wrote: > On May 9, 2016 5:31 AM, "Tim Chase" 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