[Pharo-dev] Efficient string concatenation - proposed new

Tudor Girba tudor at tudorgirba.com
Sat Nov 16 13:54:58 EST 2013


You already have it:

'' join: #('abc' 'def' 'ghi')

Doru


On Sat, Nov 16, 2013 at 7:09 PM, <btc at openinworld.com> wrote:

>
> Code Critic rule Optimization > String concatenation instead of streams
> says:
>
> "Check for string concatenation inside some iteration message. Since
> string concatenation is O(n^2), it is better to use streaming since it is
> O(n) - assuming that n is large enough. As a general principal avoid ,
> since the receiver is copied. Therefore chaining , messages will lead to
> multiple useless copies of the receiver."
>
> That is,
>    String streamContents: [:s |
>        #('abc' 'def' 'ghi')  do: [:each | s nextPutAll: each asString]]
>
> should be used instead of...
>    'abc' , 'def' , 'ghi'.
>
> However the first clutters the code.  What about something like...
>    { 'abc' . 'def' . 'ghi' } asStreamString
> where
>    Collection>>asStreamString
>        ^ String streamContents: [:s | self do: [:each | s nextPutAll: each
> asString]]
>
> cheers -ben
>
>



-- 
www.tudorgirba.com

"Every thing has its own flow"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20131116/8c80ca3f/attachment-0002.html>


More information about the Pharo-dev mailing list