String concatenation

David Fraser davidf at sjsoft.com
Wed Jun 23 04:32:17 EDT 2004


Peter Hansen wrote:
> Jonas Galvez wrote:
> 
>> Is it true that joining the string elements of a list is faster than
>> concatenating them via the '+' operator?
>>
>> "".join(['a', 'b', 'c'])
>>
>> vs
>>
>> 'a'+'b'+'c'
>>
>> If so, can anyone explain why?
> 
> 
> It's because the latter one has to build a temporary
> string consisting of 'ab' first, then the final string
> with 'c' added, while the join can (and probably does) add up
> all the lengths of the strings to be joined and build the final
> string all in one go.

Idea sprang to mind: Often (particularly in generating web pages) one 
wants to do lots of += without thinking about "".join.
So what about creating a class that will do this quickly?
The following class does this and is much faster when adding together 
lots of strings. Only seem to see performance gains above about 6000 
strings...

David

class faststr(str):
   def __init__(self, *args, **kwargs):
     self.appended = []
     str.__init__(self, *args, **kwargs)
   def __add__(self, otherstr):
     self.appended.append(otherstr)
     return self
   def getstr(self):
     return str(self) + "".join(self.appended)

def testadd(start, n):
   for i in range(n):
     start += str(i)
   if hasattr(start, "getstr"):
     return start.getstr()
   else:
     return start

if __name__ == "__main__":
   import sys
   if len(sys.argv) >= 3 and sys.argv[2] == "fast":
     start = faststr("test")
   else:
     start = "test"
   s = testadd(start, int(sys.argv[1]))



More information about the Python-list mailing list