Stringbuffer

Joshua Marshall jmarshal at mathworks.com
Sat Feb 3 22:37:35 EST 2001


chris at onca.catsden.net wrote:
> On 4 Feb 2001, Joshua Marshall wrote:

>> I'm plucking characters off a stream and concatenating them.
>> Currently, I'm doing something like:
>>
>>    buf = []
>>    while stream.more():
>>       buf = buf + stream.read()
>>    string.join(buf, '')
>>
>> Is there a faster way?  I'm thinking of writing a Stringbuffer
>> class in C.  Or does a quick, reliable one already exist?

> You could try buf.append(stream.read())

> And... I dont know what you're trying to achive with string.join(buf,'').
> Are you just adding a space?? :)

I'm using string.join to convert a list of strings into a single string.
My use of a list is for efficiency.  Intuitively, I'm trying to do:

   ret = ''
   while stream.more():
      ret = ret + stream.read()

but this is too slow.  Appending to a list is better, but I'm looking
for more speed.



More information about the Python-list mailing list