Differences between revisions 8 and 9
Revision 8 as of 2006-11-18 02:40:09
Size: 2016
Editor: 62
Comment:
Revision 9 as of 2008-11-15 13:59:38
Size: 2016
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 33: Line 33:
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)]] 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)>>
Line 54: Line 54:
-- -- MikeRovner [[DateTime(2005-08-02T10:19:06Z)]] -- -- MikeRovner <<DateTime(2005-08-02T10:19:06Z)>>
Line 56: Line 56:
 Mike, that code generates a very different (and much shorter) s. Note how the original code takes the half of the ''preconcatenated'' s, making the size grow exponentially (which generates megabytes of data). -- JürgenHermann [[DateTime(2005-08-30T18:44:05Z)]]  Mike, that code generates a very different (and much shorter) s. Note how the original code takes the half of the ''preconcatenated'' s, making the size grow exponentially (which generates megabytes of data). -- JürgenHermann <<DateTime(2005-08-30T18:44:05Z)>>
Line 59: Line 59:
-- -- DavidFord [[DateTime(2005-10-18T10:19:06Z)]] -- -- DavidFord <<DateTime(2005-10-18T10:19:06Z)>>

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.

  • -db

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 2005-08-01 06:07:51

Are you guys kidding? The whole page is contrieved. Correct implementation of "join" is:

from time import time
t = time()

s = 'lksdajflakjdsflku09uweoir'
r = [s]
for x in range(40):
    r.append(s[len(s)/2:])
s = "".join(r)

print 'duration:', time()-t

which gives on PythonWin 2.4 (#60, Nov 30 2004, 09:34:21) [MSC v.1310 32 bit (Intel)] on win32 execution times:

1st  duration: 54.4060001373
Last duration: 0.0160000324249

-- -- MikeRovner 2005-08-02 10:19:06

  • Mike, that code generates a very different (and much shorter) s. Note how the original code takes the half of the preconcatenated s, making the size grow exponentially (which generates megabytes of data). -- JürgenHermann 2005-08-30 18:44:05

-- -- DavidFord 2005-10-18 10:19:06 A few notes (your mileage may vary - this is a 4Mb file being stripped of unprintable characters)

  • Regex replacement rather than creating a list and joining it is 2.5x faster than the tooling above
  • This is far slower than the equivalent Java code (around 4x slower) using String.charAt() and StringBuffers

ConcatenationTestCode (last edited 2012-06-28 11:33:35 by cpe-24-24-211-202)

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