Revision 5 as of 2005-08-01 06:07:53

Clear message

Counter to the PythonSpeed/PerformanceTips, on python 2.4 the following string concatenation is almost twice as fast:

   1 from time import time
   2 t = time()
   3 
   4 s = 'lksdajflakjdsflku09uweoir'
   5 for x in range(40):
   6     s += s[len(s)/2:]
   7     
   8 print 'duration:', time()-t

as:

   1 from time import time
   2 t = time()
   3 
   4 s = 'lksdajflakjdsflku09uweoir'
   5 for x in range(40):
   6     s = "".join((s, s[len(s)/2:]))
   7     
   8 print 'duration:', time()-t


On the win32 Python 2.4 I'm seeing the join sample above complete in less than half the time of the concatenating sample.

Usually the join() is located outside the loop, that code makes this extremely hard though (becuase of the self-referencing of the generated string). But that situation is not the norm. -- JürgenHermann DateTime(2005-08-01T06:07:51Z)

Unable to edit the page? See the FrontPage for instructions.