On Fri, Jul 8, 2011 at 11:30 PM, Steven D'Aprano
<steve+comp.lang.pyt...@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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to