Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attributes': 0.05; 'instance': 0.05; 'python': 0.08; 'subject:string': 0.09; 'variables.': 0.09; 'wrote:': 0.15; '*very*': 0.16; 'appends': 0.16; 'billy': 0.16; 'cpython,': 0.16; 'ironpython,': 0.16; 'jython': 0.16; 'out"': 0.16; 'subject:formatting': 0.16; 'subject:vs.': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; 'cheers,': 0.19; 'cc:2**0': 0.21; 'loop': 0.22; 'cc:no real name:2**0': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'versions': 0.23; 'fri,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.30; 'instead': 0.34; 'idea': 0.36; 'optimization': 0.36; 'received:google.com': 0.38; 'received:209.85.161': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'steven': 0.38; 'think': 0.38; 'received:209': 0.40; 'results': 0.62; 'subject:. ': 0.66; 'anything,': 0.73; '100000': 0.84; '11:30': 0.84; '6.59': 0.84; 'measuring': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=2hgPUmvJpoP85QkdvnA47GJPpin3mi50qBU68pbD/RI=; b=aGMRlovEKux4vIzQj1SolFOTDt0Qtbh0ydYt57M+uxc+EdeEsNeel/Y5xdGyfPobsb 2BzzN+P1a/aN3K8xhpOzmH/dUOvfyDzL5Q4QSmXwdougeYWgWRigU7/wawzxF9/r5KWu 43U6OYzV6O8P6ltOtiwAxIJz26jelhxvqnZK4= MIME-Version: 1.0 In-Reply-To: <4e17e790$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <4e17e790$0$29977$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Sat, 9 Jul 2011 00:04:41 -0600 Subject: Re: String concatenation vs. string formatting To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1310191513 news.xs4all.nl 21804 [2001:888:2000:d::a6]:55723 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9109 On Fri, Jul 8, 2011 at 11:30 PM, Steven D'Aprano wrote: > Billy Mays wrote: > >> If it means anything, I think concatenation is faster. > > You are measuring the speed of an implementation-specific optimization. > You'll likely get *very* different results with Jython or IronPython, or > old versions of CPython, or even if you use instance attributes instead of > local variables. > > It also doesn't generalise: only appends are optimized, not prepends. Indeed: $ python -m timeit -s "v = 'x' * 10; out = ''" "out = out + v" 1000000 loops, best of 3: 6.59 usec per loop $ python -m timeit -s "v = 'x' * 10; out = ''" "out = v + out" 100000 loops, best of 3: 268 usec per loop Good to know. I had no idea such an optimization existed. Cheers, Ian