When to use a stream for concatenating text..
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!
2015-03-10 15:09 GMT-03:00 sergio_101 <sergio.rrd@gmail.com>:
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
gotcha.. this makes sense. On Tue, Mar 10, 2015 at 2:18 PM Esteban A. Maringolo <emaringolo@gmail.com> wrote:
2015-03-10 15:09 GMT-03:00 sergio_101 <sergio.rrd@gmail.com>:
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
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 <sergio.rrd@gmail.com> 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!
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" <btc@openinworld.com> 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 <sergio.rrd@gmail.com> 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!
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!
On 13 Mar 2015, at 08:41, stepharo <stepharo@free.fr> 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
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 <marcus.denker@inria.fr> wrote:
On 13 Mar 2015, at 08:41, stepharo <stepharo@free.fr> 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
participants (6)
-
Ben Coman -
Esteban A. Maringolo -
Marcus Denker -
sergio_101 -
stepharo -
Sven Van Caekenberghe