sum and strings

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


Alex Martelli wrote:

> In terms of performance, however, the simple loop that you (rhamph)
> posted is generally best -- e.g., with Python 2.5c1 on a MacbookPro:
> 
> brain:~/downloads alex$ python -mtimeit -s'deep=[range(9)]*9'
> 's=sum(deep,[])'
> 100000 loops, best of 3: 11.2 usec per loop
> 
> brain:~/downloads alex$ python -mtimeit -s'deep=[range(9)]*9' 's=[]
>> for sublist in deep: s.extend(sublist)'
> 100000 loops, best of 3: 6.92 usec per loop

at least on this machine, map(s.extend) is slightly faster than the loop:

timeit -s"deep=[range(9)]*9" "s=[]" "for sublist in deep:
  s.extend(sublist)"
100000 loops, best of 3: 5.59 usec per loop

timeit -s"deep=[range(9)]*9" "s=[]" "map(s.extend, deep)"
100000 loops, best of 3: 5.26 usec per loop

</F>




More information about the Python-list mailing list