Bitwise OR?

Jean-Paul Calderone exarkun at divmod.com
Fri Mar 24 10:55:37 EST 2006


On Fri, 24 Mar 2006 15:40:17 +0100, "Diez B. Roggisch" <deets at nospam.web.de> wrote:
>Tim N. van der Leeuw wrote:
>> I also wonder if it wouldn't be faster to put the numbers into a list
>> and join the list into a string -- did you test with that?
>
>It will be faster - the naive string concatenation is quadratic, whereas the
>list-based approach is linear.

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
$ 

Jean-Paul



More information about the Python-list mailing list