Prev: StringChain -- a data structure for managing large sequences of chunks of bytes
Next: StringChain -- a data structure for managing large sequencesof chunks of bytes
From: Paul Rubin on 12 Mar 2010 02:20
"Zooko O'Whielacronx" <zookog(a)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()
result = buf.getvalue()
Sometimes I like to use a generator instead:
yield "first thing"
yield "second thing"
result = ''.join(stuff())