When to use a stream for concatenating text..

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

When to use a stream for concatenating text..

sergio_101

it seems that in more cases than not, i find that developers use a stream when concatenating some text strings.

I am wondering if this is a smalltalk thing, or is there a real speed benefit when using streams in this way.

Thanks!

Reply | Threaded
Open this post in threaded view
|

Re: When to use a stream for concatenating text..

Esteban A. Maringolo
2015-03-10 15:09 GMT-03:00 sergio_101 <[hidden email]>:
>
> it seems that in more cases than not, i find that developers use a stream
> when concatenating some text strings.
>
> I am wondering if this is a smalltalk thing, or is there a real speed
> benefit when using streams in this way.

It is not a matter of speed only, but mostly memory.

For large text concatenations Streams are more memory efficient
because you don't create intermediate strings.

E.g.
'string1', 'string2', 'string3' ... , "stringN" will require the
creation of N intermediate String instances.

aStream
  nextPutAll: 'string1';
  nextPutAll: 'string2';
  nextPutAll: 'string3';
  nextPutAll: 'stringN'

Will only add contents to the Stream collection.


Esteban A. Maringolo

Reply | Threaded
Open this post in threaded view
|

Re: When to use a stream for concatenating text..

sergio_101
gotcha.. this makes sense.

On Tue, Mar 10, 2015 at 2:18 PM Esteban A. Maringolo <[hidden email]> wrote:
2015-03-10 15:09 GMT-03:00 sergio_101 <[hidden email]>:
>
> it seems that in more cases than not, i find that developers use a stream
> when concatenating some text strings.
>
> I am wondering if this is a smalltalk thing, or is there a real speed
> benefit when using streams in this way.

It is not a matter of speed only, but mostly memory.

For large text concatenations Streams are more memory efficient
because you don't create intermediate strings.

E.g.
'string1', 'string2', 'string3' ... , "stringN" will require the
creation of N intermediate String instances.

aStream
  nextPutAll: 'string1';
  nextPutAll: 'string2';
  nextPutAll: 'string3';
  nextPutAll: 'stringN'

Will only add contents to the Stream collection.


Esteban A. Maringolo

Reply | Threaded
Open this post in threaded view
|

Re: When to use a stream for concatenating text..

Ben Coman
In reply to this post by sergio_101
There are a few things floating around the web saying that it is faster using a stream for concatenation, but its been shown a while back to be "not necessarily" true.  It depends on the use case so you should profile - if its that important.
cheers -ben

On Wed, Mar 11, 2015 at 2:09 AM, sergio_101 <[hidden email]> wrote:

it seems that in more cases than not, i find that developers use a stream when concatenating some text strings.

I am wondering if this is a smalltalk thing, or is there a real speed benefit when using streams in this way.

Thanks!


Reply | Threaded
Open this post in threaded view
|

Re: When to use a stream for concatenating text..

Esteban A. Maringolo

Andres Valloud performed some benchmarking in the past and his conclusion was that there was a threshold higher than expected when the streamed concatenation was faster and memory savvier. This was years ago, and of course I don't remember such threshold.

However from the code aesthetics I use simple concatenation when I know beforehand there will be two or three semantically equivalent strings to be joined and choose streams when there is an uncertain lenght concatenation  (most of the times involving appending while iterating).

Not to mention implementations where the stream is passed as an argument, which in my experience are easier to factor.

El mar 10, 2015 8:06 PM, "Ben Coman" <[hidden email]> escribió:
There are a few things floating around the web saying that it is faster using a stream for concatenation, but its been shown a while back to be "not necessarily" true.  It depends on the use case so you should profile - if its that important.
cheers -ben

On Wed, Mar 11, 2015 at 2:09 AM, sergio_101 <[hidden email]> wrote:

it seems that in more cases than not, i find that developers use a stream when concatenating some text strings.

I am wondering if this is a smalltalk thing, or is there a real speed benefit when using streams in this way.

Thanks!


Reply | Threaded
Open this post in threaded view
|

Re: When to use a stream for concatenating text..

stepharo
In reply to this post by sergio_101
use

String streamContents: [:s |
     s nextPutAll: 'jlklkjkl' ]


or

String streamContents: [:s |
     s << 'jlklkjkl' ]


it is a great method for manipulating

Le 10/3/15 19:09, sergio_101 a écrit :
>
> it seems that in more cases than not, i find that developers use a
> stream when concatenating some text strings.
>
> I am wondering if this is a smalltalk thing, or is there a real speed
> benefit when using streams in this way.
>
> Thanks!
>


Reply | Threaded
Open this post in threaded view
|

Re: When to use a stream for concatenating text..

Marcus Denker-4

> On 13 Mar 2015, at 08:41, stepharo <[hidden email]> wrote:
>
> use
>
> String streamContents: [:s |
>    s nextPutAll: 'jlklkjkl' ]
>
>
> or
>
> String streamContents: [:s |
>    s << 'jlklkjkl' ]
>
>
> it is a great method for manipulating
>
> Le 10/3/15 19:09, sergio_101 a écrit :
>>
>> it seems that in more cases than not, i find that developers use a stream when concatenating some text strings.
>>
>> I am wondering if this is a smalltalk thing, or is there a real speed benefit when using streams in this way.
>>

When adding to a string, you create a new one with the right size and copy over the old content.

Using a stream avoids that. But there is a cost for handling the stream, too:

[String streamContents: [:s |
   s nextPutAll: 'jlklkjkl';
         nextPutAll: 'jlklkjkl' ]] bench. "'1,866,774 per second'"


['jlklkjkl' ,  'jlklkjkl'] bench "'5,433,931 per second’"


So it is only faster e.g. when adding to a string in a loop or on larger strings.
But it reads quite nice, too, so I tend to use it without thinking about performance.

        Marcus



Reply | Threaded
Open this post in threaded view
|

Re: When to use a stream for concatenating text..

Sven Van Caekenberghe-2
The breaking point seems to be around 5 concatenations:

str:= Character alphabet.
[ str, str, str, str, str ] bench.

  "'1,020,043 per second'"

[ String new: str size * 5 streamContents: [ :out | 5 timesRepeat: [ out nextPutAll: str ] ] ] bench.

  "'949,738 per second'"

> On 13 Mar 2015, at 08:52, Marcus Denker <[hidden email]> wrote:
>
>>
>> On 13 Mar 2015, at 08:41, stepharo <[hidden email]> wrote:
>>
>> use
>>
>> String streamContents: [:s |
>>   s nextPutAll: 'jlklkjkl' ]
>>
>>
>> or
>>
>> String streamContents: [:s |
>>   s << 'jlklkjkl' ]
>>
>>
>> it is a great method for manipulating
>>
>> Le 10/3/15 19:09, sergio_101 a écrit :
>>>
>>> it seems that in more cases than not, i find that developers use a stream when concatenating some text strings.
>>>
>>> I am wondering if this is a smalltalk thing, or is there a real speed benefit when using streams in this way.
>>>
>
> When adding to a string, you create a new one with the right size and copy over the old content.
>
> Using a stream avoids that. But there is a cost for handling the stream, too:
>
> [String streamContents: [:s |
>   s nextPutAll: 'jlklkjkl';
> nextPutAll: 'jlklkjkl' ]] bench. "'1,866,774 per second'"
>
>
> ['jlklkjkl' ,  'jlklkjkl'] bench "'5,433,931 per second’"
>
>
> So it is only faster e.g. when adding to a string in a loop or on larger strings.
> But it reads quite nice, too, so I tend to use it without thinking about performance.
>
> Marcus