Stringbuffer

Joshua Marshall jmarshal at mathworks.com
Sat Feb 3 22:44:21 EST 2001


Joshua Marshall <jmarshal at mathworks.com> wrote:
> 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.

Oh - and my mistake, I was typing the code from memory.  My first example
errors if stream.read() returns a string.  But you see my intent.



More information about the Python-list mailing list