Nested function scope problem

Justin Azoff justin.azoff at gmail.com
Sat Jul 22 12:20:14 EDT 2006


Josiah Manson wrote:
> I just did some timings, and found that using a list instead of a
> string for tok is significantly slower (it takes 1.5x longer). Using a
> regex is slightly faster for long strings, and slightly slower for
> short ones. So, regex wins in both berevity and speed!

I think the list.append method of building strings may only give you
speed improvements when you are adding bigger chunks of strings
together instead of 1 character at a time. also:

http://docs.python.org/whatsnew/node12.html#SECTION0001210000000000000000

"""String concatenations in statements of the form s = s + "abc" and s
+= "abc" are now performed more efficiently in certain circumstances.
This optimization won't be present in other Python implementations such
as Jython, so you shouldn't rely on it; using the join() method of
strings is still recommended when you want to efficiently glue a large
number of strings together. (Contributed by Armin Rigo.)"""

I tested both, and these are my results for fairly large strings:

justin at latitude:/tmp$ python /usr/lib/python2.4/timeit.py -s'import
foo' 'foo.test(foo.breakLine)'
10 loops, best of 3: 914 msec per loop

justin at latitude:/tmp$ python /usr/lib/python2.4/timeit.py -s'import
foo' 'foo.test(foo.breakLineRE)'
10 loops, best of 3: 289 msec per loop

-- 
- Justin




More information about the Python-list mailing list