StringChain -- a data structure for managing large sequences of chunks of bytes

Paul Rubin no.email at nospam.invalid
Fri Mar 12 02:20:08 EST 2010


"Zooko O'Whielacronx" <zookog at gmail.com> writes:
> Every couple of years I run into a problem where some Python code that
> worked well at small scales starts burning up my CPU at larger scales,
> and the underlying issue turns out to be the idiom of accumulating
> data by string concatenation. 

I usually use StringIO or cStringIO for that (python 2.x syntax):

   buf = cStringIO()
   buf.write("first thing")
   buf.write("second thing")
   result = buf.getvalue()

Sometimes I like to use a generator instead:

  def stuff():
     yield "first thing"
     yield "second thing"

  result = ''.join(stuff())



More information about the Python-list mailing list