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.den...@inria.fr> wrote: > >> >> On 13 Mar 2015, at 08:41, stepharo <steph...@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