String concatenation vs. string formatting
ian.g.kelly at gmail.com
Sat Jul 9 02:04:41 EDT 2011
On Fri, Jul 8, 2011 at 11:30 PM, Steven D'Aprano
<steve+comp.lang.python at 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.
$ 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.
More information about the Python-list