Bitwise OR?

Diez B. Roggisch deets at nospam.web.de
Fri Mar 24 11:22:42 EST 2006


> It won't be any faster in CPython 2.4 or newer.  This kind of string
> concatenation is optimized so as to be linear:
> 
> $ python -m timeit -s "x = ''" "for i in xrange(1000): x += 'x'"
> 1000 loops, best of 3: 335 usec per loop
> $ python -m timeit -s "x = ''" "for i in xrange(10000): x += 'x'"
> 100 loops, best of 3: 3.35 msec per loop
> $ python -m timeit -s "x = ''" "for i in xrange(100000): x += 'x'"
> 10 loops, best of 3: 33.2 msec per loop
> $ python -m timeit -s "x = ''" "for i in xrange(1000000): x += 'x'"
> 10 loops, best of 3: 347 msec per loop
> $ python -m timeit -s "x = ''" "for i in xrange(10000000): x += 'x'"
> 10 loops, best of 3: 3.48 sec per loop


Interesting. I wasn't aware of that optimization.

diez



More information about the Python-list mailing list