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


Groups > comp.lang.python > #9109

Re: String concatenation vs. string formatting

References <mailman.785.1310156331.1164.python-list@python.org> <iv7qv2$2ar$1@speranza.aioe.org> <4e17e790$0$29977$c3e8da3$5496439d@news.astraweb.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-07-09 00:04 -0600
Subject Re: String concatenation vs. string formatting
Newsgroups comp.lang.python
Message-ID <mailman.793.1310191513.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jul 8, 2011 at 11:30 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> 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

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


Thread

String concatenation vs. string formatting Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-08 15:18 -0500
  Re: String concatenation vs. string formatting John Gordon <gordon@panix.com> - 2011-07-08 20:23 +0000
    Re: String concatenation vs. string formatting Ben Finney <ben+python@benfinney.id.au> - 2011-07-09 08:50 +1000
      Re: String concatenation vs. string formatting Ben Finney <ben+python@benfinney.id.au> - 2011-07-09 09:15 +1000
    Re: String concatenation vs. string formatting Thorsten Kampe <thorsten@thorstenkampe.de> - 2011-07-09 06:23 +0200
    Re: String concatenation vs. string formatting Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-10 01:45 -0500
      Re: String concatenation vs. string formatting Ben Finney <ben+python@benfinney.id.au> - 2011-07-10 17:07 +1000
      Re: String concatenation vs. string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-10 23:42 +1000
      Re: String concatenation vs. string formatting Roy Smith <roy@panix.com> - 2011-07-10 10:33 -0400
        Re: String concatenation vs. string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-11 08:48 +1000
        Re: String concatenation vs. string formatting Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-10 18:24 -0500
  Re: String concatenation vs. string formatting Billy Mays <noway@nohow.com> - 2011-07-08 16:57 -0400
    Re: String concatenation vs. string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-09 15:30 +1000
      Re: String concatenation vs. string formatting Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-09 00:04 -0600
      Re: String concatenation vs. string formatting Chris Angelico <rosuav@gmail.com> - 2011-07-09 16:16 +1000
      Re: String concatenation vs. string formatting Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-09 00:29 -0600
  Re: String concatenation vs. string formatting Ben Finney <ben+python@benfinney.id.au> - 2011-07-09 08:59 +1000
    Re: String concatenation vs. string formatting Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-08 18:29 -0500
  Re: String concatenation vs. string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-09 15:12 +1000

csiph-web