sum and strings

Fredrik Lundh fredrik at pythonware.com
Mon Aug 21 06:55:14 EDT 2006


Steven D'Aprano wrote:

> That's a nonsense argument. There is no shortage of slow algorithms, and
> some of them are built into Python. When did O(n**2) become an error
> condition? And overhead matters: if I'm only doing a few concatenations,
> it is significantly faster to add the strings using + than to use join()
> (at least in Python 2.3 -- YMMV):
> 
>>>> s = timeit.Timer("''.join(L)", "L=['a'*50, 'b'*50, 'c'*50]")
>>>> s.timeit()
> 1.3470098972320557
>>>> t = timeit.Timer("a+b+c", "a,b,c = 'a'*50, 'b'*50, 'c'*50")
>>>> t.timeit()
> 1.0698421001434326

and what exactly does the fact that Python can do operator-based 
dispatch much faster than it can do method-based dispatch have to
do with sum's inability to add strings ?

did you have some kind of "zero overhead for some function calls" 
optimization in mind ?

</F>




More information about the Python-list mailing list